Sparrow SMS Phone Verification API 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.
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
Django doesn't have
urls.pyby default. Devs create it later as per needs. It helps us manage all routes, url paths and patterns of an app within a file.
imports, the most basic module to import is
path that returns for
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
Django's URLconf Notes from Docs:
- There’s no need to add a leading slash because every URL has that. For example, it’s
pathmatches 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
search_fieldsenables a search box on the user list page.
list_filterenables a filter function in the right sidebar of the page.
list_displayallows you to choose the attributes from the database to display on the admin site. Specifically, we've included
phone_numberto 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 into
XMLor other content types.
The serializers in REST framework work very similarly to Django's
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.
NOTE: This is Part 2 of a multi-part series.
Part 3 is here.