SMS Phone Verification in Django - Part 2
Starting off imports, the most basic module to import is path that returns for urlpatterns. ValidatePhoneSendOTP and ValidateOTP are API view classes that subclass Django's View class. These will arrive later in Step 5: Writing Final Views part of this tutorial.
Introduction
This is a multi-part series on how to integrate Sparrow SMS phone verification api in a django powered web app. Here are Part 1 and Part 3.
Step 2 — Registering patterns to urls.py
After writing the necessary database models and attributes for our phone database, we create a new urls.py
file at the root of our Django app myappname
.
Django doesn't have urls.py
by default. Devs create it later as per needs. It helps us manage all routes, url paths and patterns of an app within a file.
Starting off imports
, the most basic module to import is path
that returns for urlpatterns
. ValidatePhoneSendOTP
and ValidateOTP
are API view classes that subclass Django's View
class. These will arrive later in Step 5: Writing Final Views
part of this tutorial.
app_name
is crucial to let Django know that urlpatterns
we write below it should work with the app name specified.
Hence, line 7 sets ''
root of the relative path to return ValidatePhoneSendOTP
. And line 8 sets verify/
in the relative path to return ValidateOTP
.
Django's URLconf Notes from Docs:
- There’s no need to add a leading slash because every URL has that. For example, it’s
articles
, not/articles
. path
matches any non-empty string, including the path separator,/
.
Step 3 - Customize django-admin using admin.py
django-admin
is a command-line utility package with an admin dashboard that comes battery included in Django framework. Here we use @
to use the register
decorator to register our AccountUserAdmin
class and customize with ModelAdmin
options.
search_fields
enables a search box on the user list page.list_filter
enables a filter function in the right sidebar of the page.list_display
allows you to choose the attributes from the database to display on the admin site. Specifically, we've includedphone_verified
andphone_number
to display on our admin site. So this would allow webmasters to see database entries directly updated in the UI of the admin site.
Step 4 - Render using serializers.py
Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily rendered intoJSON
,XML
or other content types.
The serializers in REST framework work very similarly to Django'sForm
andModelForm
classes. -Django REST framework
All we are trying to do is serialize the contents of the imported get_user_model()
and activate a function that sets and returns the instance of the email address of the user as verified.
email_verified
is set to read-only so that the integrity and verification are preserved well and true to unauthorized changes from outside the system.
This is a multi-part series on how to integrate Sparrow SMS phone verification api in a Django powered web app. Here are Part 1 and Part 3.