<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by EBIN JOSEPH on Medium]]></title>
        <description><![CDATA[Stories by EBIN JOSEPH on Medium]]></description>
        <link>https://medium.com/@ebin7joseph?source=rss-6f3cf81d6df1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*gp2RJsnu6sf6JpEQ.</url>
            <title>Stories by EBIN JOSEPH on Medium</title>
            <link>https://medium.com/@ebin7joseph?source=rss-6f3cf81d6df1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 17:03:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ebin7joseph/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Creating a Project Template with Django Rest Framework with JWT Authentication]]></title>
            <link>https://medium.com/@ebin7joseph/django-rest-framework-authentication-with-jwt-8687494509d8?source=rss-6f3cf81d6df1------2</link>
            <guid isPermaLink="false">https://medium.com/p/8687494509d8</guid>
            <category><![CDATA[django-rest-framework]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[jwt]]></category>
            <category><![CDATA[rest-api]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[EBIN JOSEPH]]></dc:creator>
            <pubDate>Sat, 09 Oct 2021 20:05:36 GMT</pubDate>
            <atom:updated>2021-10-10T10:22:52.999Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*UKPpXkDaYLtD8pkuVB9QeQ.jpeg" /></figure><p>In most of the small projects, security and authentication is mostly the final topic to be thought about. Usually only after the most important features are built, does developers start to think about implementing logins and user management. And if features are not implemented with authentication in mind, developers most likely have to rewrite the code to factor in security. This problem is most prevalent in fast developed prototype applications which are probably developed with django or flask.</p><p>A solution which I found to work is to build template projects containing some basic functionality like token authentication, user management, logging etc…</p><p>Since I am not starting from scratch every time I create a new prototype, I can stop thinking about authentication and user management and focus on important use-cases.</p><h3><strong>Project Setup</strong></h3><p>In this tutorial, we build one such REST API with Django Rest Framework as back-end. We will see how to make it a template using <em>cookiecutter </em>tool. We will be using JWT(JSON Web Tokens) for authentication.</p><p>To start,</p><p>create a python virtual environment with a environment manager of your choice.</p><p>After that install the required libraries in the environment with pip :</p><pre>pip install django djangorestframework djangorestframework-jwt django-cors-headers</pre><p>Then create a django project with <em>django-admin </em>command:</p><pre>django-admin startproject backend</pre><p>Afterwards change directory to the project you created:</p><pre>cd backend</pre><p>Now that the project file structure is created, we can create a django app to handle users.</p><pre>python manage.py startapp users</pre><p>This will create the directory structure required for the app users.</p><p>Now open up the <em>settings.py</em> file in <em>backend</em> folder and add the created user app and the installed libraries to the <em>INSTALLED_APPS</em> array</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dvscode%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3DINSTALLED_APPS%252520%25253D%252520%25255B%25250A%252520%252520%252520%252520%252522django.contrib.admin%252522%25252C%25250A%252520%252520%252520%252520%252522django.contrib.auth%252522%25252C%25250A%252520%252520%252520%252520%252522django.contrib.contenttypes%252522%25252C%25250A%252520%252520%252520%252520%252522django.contrib.sessions%252522%25252C%25250A%252520%252520%252520%252520%252522django.contrib.messages%252522%25252C%25250A%252520%252520%252520%252520%252522django.contrib.staticfiles%252522%25252C%25250A%252520%252520%252520%252520%252522rest_framework%252522%25252C%25250A%252520%252520%252520%252520%252522corsheaders%252522%25252C%25250A%252520%252520%252520%252520%252522users%252522%25252C%25250A%25255D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dvscode%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3DINSTALLED_APPS%25252520%2525253D%25252520%2525255B%2525250A%25252520%25252520%25252520%25252520%25252522django.contrib.admin%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252522django.contrib.auth%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252522django.contrib.contenttypes%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252522django.contrib.sessions%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252522django.contrib.messages%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252522django.contrib.staticfiles%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252522rest_framework%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252522corsheaders%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252522users%25252522%2525252C%2525250A%2525255D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c8f33fa498fe9a057b9a32f1e699ba5/href">https://medium.com/media/5c8f33fa498fe9a057b9a32f1e699ba5/href</a></iframe><p>Next setup debug configuration.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dvscode%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3DALLOWED_HOSTS%252520%25253D%252520%25255B%25255D%25250Aif%252520not%252520DEBUG%25253A%25250A%252520%252520%252520%252520logging.info%2528%252522Debugging%252520is%252520not%252520enabled.%252522%2529%25250A%252520%252520%252520%252520ALLOWED_HOSTS%252520%25253D%252520%25255B%252522%252A%252522%25255D%25250A%252520%252520%252520%252520LOG_LEVEL%252520%25253D%252520%252522ERROR%252522%25250Aelse%25253A%25250A%252520%252520%252520%252520logging.info%2528%252522Debugging%252520is%252520enabled.%252522%2529%25250A%252520%252520%252520%252520ALLOWED_HOSTS%252520%25253D%252520%25255B%252522%252A%252522%25255D%25250A%252520%252520%252520%252520LOG_LEVEL%252520%25253D%252520%252522DEBUG%252522&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dvscode%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3DALLOWED_HOSTS%25252520%2525253D%25252520%2525255B%2525255D%2525250Aif%25252520not%25252520DEBUG%2525253A%2525250A%25252520%25252520%25252520%25252520logging.info%252528%25252522Debugging%25252520is%25252520not%25252520enabled.%25252522%252529%2525250A%25252520%25252520%25252520%25252520ALLOWED_HOSTS%25252520%2525253D%25252520%2525255B%25252522%25252A%25252522%2525255D%2525250A%25252520%25252520%25252520%25252520LOG_LEVEL%25252520%2525253D%25252520%25252522ERROR%25252522%2525250Aelse%2525253A%2525250A%25252520%25252520%25252520%25252520logging.info%252528%25252522Debugging%25252520is%25252520enabled.%25252522%252529%2525250A%25252520%25252520%25252520%25252520ALLOWED_HOSTS%25252520%2525253D%25252520%2525255B%25252522%25252A%25252522%2525255D%2525250A%25252520%25252520%25252520%25252520LOG_LEVEL%25252520%2525253D%25252520%25252522DEBUG%25252522&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/d578d34249adaa2ee971a84b2749368e/href">https://medium.com/media/d578d34249adaa2ee971a84b2749368e/href</a></iframe><p>After that we will setup logging configuration by adding the following code to end of <em>settings.py</em> file.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528164%252C184%252C199%252C1%2529%26t%3Dvscode%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3DLOGGING%252520%25253D%252520%25257B%25250A%252520%252520%252520%252520%252522version%252522%25253A%2525201%25252C%25250A%252520%252520%252520%252520%252522disable_existing_loggers%252522%25253A%252520False%25252C%25250A%252520%252520%252520%252520%252522formatters%252522%25253A%252520%25257B%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252522console%252522%25253A%252520%25257B%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252522format%252522%25253A%252520%252522%252525%2528asctime%2529s%252520%252525%2528name%2529-12s%252520%252525%2528levelname%2529-8s%252520%252525%2528message%2529s%252522%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%25257D%25252C%25250A%252520%252520%252520%252520%25257D%25252C%25250A%252520%252520%252520%252520%252522handlers%252522%25253A%252520%25257B%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252522console%252522%25253A%252520%25257B%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252522class%252522%25253A%252520%252522logging.StreamHandler%252522%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252522formatter%252522%25253A%252520%252522console%252522%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%25257D%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252523%252520%252522file%252522%25253A%252520%25257B%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252523%252520%252520%252520%252520%252520%252522class%252522%25253A%252520%252522logging.FileHandler%252522%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252523%252520%252520%252520%252520%252520%252522filename%252522%25253A%252520%252522logs%25252Fdebug.log%252522%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252523%252520%252520%252520%252520%252520%252522formatter%252522%25253A%252520%252522console%252522%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252523%252520%25257D%25252C%25250A%252520%252520%252520%252520%25257D%25252C%25250A%252520%252520%252520%252520%252522loggers%252522%25253A%252520%25257B%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252522%252522%25253A%252520%25257B%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252522level%252522%25253A%252520LOG_LEVEL%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252522handlers%252522%25253A%252520%25255B%252522console%252522%25255D%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%25257D%25252C%25250A%252520%252520%252520%252520%25257D%25252C%25250A%25257D%25250A%25250Alogging.config.dictConfig%2528LOGGING%2529&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528164%25252C184%25252C199%25252C1%252529%26t%3Dvscode%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3DLOGGING%25252520%2525253D%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252522version%25252522%2525253A%252525201%2525252C%2525250A%25252520%25252520%25252520%25252520%25252522disable_existing_loggers%25252522%2525253A%25252520False%2525252C%2525250A%25252520%25252520%25252520%25252520%25252522formatters%25252522%2525253A%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252522console%25252522%2525253A%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252522format%25252522%2525253A%25252520%25252522%25252525%252528asctime%252529s%25252520%25252525%252528name%252529-12s%25252520%25252525%252528levelname%252529-8s%25252520%25252525%252528message%252529s%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525257D%2525252C%2525250A%25252520%25252520%25252520%25252520%2525257D%2525252C%2525250A%25252520%25252520%25252520%25252520%25252522handlers%25252522%2525253A%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252522console%25252522%2525253A%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252522class%25252522%2525253A%25252520%25252522logging.StreamHandler%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252522formatter%25252522%2525253A%25252520%25252522console%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525257D%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252523%25252520%25252522file%25252522%2525253A%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252523%25252520%25252520%25252520%25252520%25252520%25252522class%25252522%2525253A%25252520%25252522logging.FileHandler%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252523%25252520%25252520%25252520%25252520%25252520%25252522filename%25252522%2525253A%25252520%25252522logs%2525252Fdebug.log%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252523%25252520%25252520%25252520%25252520%25252520%25252522formatter%25252522%2525253A%25252520%25252522console%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252523%25252520%2525257D%2525252C%2525250A%25252520%25252520%25252520%25252520%2525257D%2525252C%2525250A%25252520%25252520%25252520%25252520%25252522loggers%25252522%2525253A%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252522%25252522%2525253A%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252522level%25252522%2525253A%25252520LOG_LEVEL%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252522handlers%25252522%2525253A%25252520%2525255B%25252522console%25252522%2525255D%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525257D%2525252C%2525250A%25252520%25252520%25252520%25252520%2525257D%2525252C%2525250A%2525257D%2525250A%2525250Alogging.config.dictConfig%252528LOGGING%252529&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/d6e08d96c70e1943d51cd91e234a2a0e/href">https://medium.com/media/d6e08d96c70e1943d51cd91e234a2a0e/href</a></iframe><p>If you want the logs to be written to a file, you can uncomment the file in the handlers and add file to handlers array.</p><p>Next we will add the CORS configuration.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528164%252C184%252C199%252C1%2529%26t%3Dvscode%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3DCORS_ALLOW_HEADERS%252520%25253D%252520list%2528default_headers%2529%252520%25252B%252520%25255B%25250A%252520%252520%252520%252520%252522observe%252522%25252C%25250A%25255D%25250ACORS_ORIGIN_ALLOW_ALL%252520%25253D%252520True%25250ACORS_ALLOW_CREDENTIALS%252520%25253D%252520True%25250ACORS_ORIGIN_WHITELIST%252520%25253D%252520%25255B%25250A%252520%252520%252520%252520%252522http%25253A%25252F%25252Flocalhost%25253A4200%252522%25252C%25250A%25255D%25250ACORS_ORIGIN_REGEX_WHITELIST%252520%25253D%252520%25255B%25250A%252520%252520%252520%252520%252522http%25253A%25252F%25252Flocalhost%25253A4200%252522%25252C%25250A%25255D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528164%25252C184%25252C199%25252C1%252529%26t%3Dvscode%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3DCORS_ALLOW_HEADERS%25252520%2525253D%25252520list%252528default_headers%252529%25252520%2525252B%25252520%2525255B%2525250A%25252520%25252520%25252520%25252520%25252522observe%25252522%2525252C%2525250A%2525255D%2525250ACORS_ORIGIN_ALLOW_ALL%25252520%2525253D%25252520True%2525250ACORS_ALLOW_CREDENTIALS%25252520%2525253D%25252520True%2525250ACORS_ORIGIN_WHITELIST%25252520%2525253D%25252520%2525255B%2525250A%25252520%25252520%25252520%25252520%25252522http%2525253A%2525252F%2525252Flocalhost%2525253A4200%25252522%2525252C%2525250A%2525255D%2525250ACORS_ORIGIN_REGEX_WHITELIST%25252520%2525253D%25252520%2525255B%2525250A%25252520%25252520%25252520%25252520%25252522http%2525253A%2525252F%2525252Flocalhost%2525253A4200%25252522%2525252C%2525250A%2525255D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/d65ac2a1e12bc6c8311f530c1e47c485/href">https://medium.com/media/d65ac2a1e12bc6c8311f530c1e47c485/href</a></iframe><p>Now for the CORS to work, we need to add the corresponding middleware.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528164%252C184%252C199%252C1%2529%26t%3Dvscode%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3DMIDDLEWARE%252520%25253D%252520%25255B%25250A%252520%252520%252520%252520%252522django.middleware.security.SecurityMiddleware%252522%25252C%25250A%252520%252520%252520%252520%252522django.contrib.sessions.middleware.SessionMiddleware%252522%25252C%25250A%252520%252520%252520%252520%252522django.middleware.common.CommonMiddleware%252522%25252C%25250A%252520%252520%252520%252520%252522corsheaders.middleware.CorsMiddleware%252522%25252C%25250A%252520%252520%252520%252520%252522django.middleware.csrf.CsrfViewMiddleware%252522%25252C%25250A%252520%252520%252520%252520%252522django.contrib.auth.middleware.AuthenticationMiddleware%252522%25252C%25250A%252520%252520%252520%252520%252522django.contrib.messages.middleware.MessageMiddleware%252522%25252C%25250A%252520%252520%252520%252520%252522django.middleware.clickjacking.XFrameOptionsMiddleware%252522%25252C%25250A%25255D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528164%25252C184%25252C199%25252C1%252529%26t%3Dvscode%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3DMIDDLEWARE%25252520%2525253D%25252520%2525255B%2525250A%25252520%25252520%25252520%25252520%25252522django.middleware.security.SecurityMiddleware%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252522django.contrib.sessions.middleware.SessionMiddleware%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252522django.middleware.common.CommonMiddleware%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252522corsheaders.middleware.CorsMiddleware%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252522django.middleware.csrf.CsrfViewMiddleware%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252522django.contrib.auth.middleware.AuthenticationMiddleware%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252522django.contrib.messages.middleware.MessageMiddleware%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252522django.middleware.clickjacking.XFrameOptionsMiddleware%25252522%2525252C%2525250A%2525255D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/241941e949766364bc73436d0e87eb79/href">https://medium.com/media/241941e949766364bc73436d0e87eb79/href</a></iframe><p>Here is the <em>settings.py</em> file with all additions until now.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e5aaeb82e14cd0af7771d362ab172c59/href">https://medium.com/media/e5aaeb82e14cd0af7771d362ab172c59/href</a></iframe><h3><strong>Creating Entities</strong></h3><p>Now that we have configured the settings for a bare minimum setup, we can start creating our user app.</p><p>For that we will open the user directory and open <em>models.py</em> to create out user entity. Django already provides a builtin user entity for us, but that entity is limited if we want have more fields in the user entity to store more data about the user. Fortunately Django already provides us with base classes to inherit from and create our own user entity.</p><p>So using the classes that django provides, we will create a user:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528164%252C184%252C199%252C1%2529%26t%3Dvscode%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dclass%252520User%2528AbstractBaseUser%2529%25253A%25250A%25250A%252520%252520%252520%252520id%252520%25253D%252520models.UUIDField%2528primary_key%25253DTrue%25252C%252520default%25253Duuid.uuid4%25252C%252520editable%25253DFalse%2529%25250A%252520%252520%252520%252520email%252520%25253D%252520models.EmailField%2528verbose_name%25253D%252522email%252520address%252522%25252C%252520max_length%25253D255%25252C%252520unique%25253DTrue%2529%25250A%252520%252520%252520%252520is_active%252520%25253D%252520models.BooleanField%2528default%25253DTrue%2529%25250A%252520%252520%252520%252520is_staff%252520%25253D%252520models.BooleanField%2528default%25253DFalse%2529%25250A%252520%252520%252520%252520is_superuser%252520%25253D%252520models.BooleanField%2528default%25253DFalse%2529%25250A%252520%252520%252520%252520USERNAME_FIELD%252520%25253D%252520%252522email%252522%25250A%252520%252520%252520%252520REQUIRED_FIELDS%252520%25253D%252520%25255B%25255D%25250A%25250A%252520%252520%252520%252520%252523%252520Tells%252520Django%252520that%252520the%252520UserManager%252520class%252520defined%252520above%252520should%252520manage%25250A%252520%252520%252520%252520%252523%252520objects%252520of%252520this%252520type.%25250A%252520%252520%252520%252520objects%252520%25253D%252520UserManager%2528%2529%25250A%25250A%252520%252520%252520%252520def%252520__str__%2528self%2529%25253A%25250A%252520%252520%252520%252520%252520%252520%252520%252520return%252520self.email%25250A%25250A%252520%252520%252520%252520class%252520Meta%25253A%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252522%252522%252522%25250A%252520%252520%252520%252520%252520%252520%252520%252520to%252520set%252520table%252520name%252520in%252520database%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252522%252522%252522%25250A%25250A%252520%252520%252520%252520%252520%252520%252520%252520db_table%252520%25253D%252520%252522login%252522&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528164%25252C184%25252C199%25252C1%252529%26t%3Dvscode%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dclass%25252520User%252528AbstractBaseUser%252529%2525253A%2525250A%2525250A%25252520%25252520%25252520%25252520id%25252520%2525253D%25252520models.UUIDField%252528primary_key%2525253DTrue%2525252C%25252520default%2525253Duuid.uuid4%2525252C%25252520editable%2525253DFalse%252529%2525250A%25252520%25252520%25252520%25252520email%25252520%2525253D%25252520models.EmailField%252528verbose_name%2525253D%25252522email%25252520address%25252522%2525252C%25252520max_length%2525253D255%2525252C%25252520unique%2525253DTrue%252529%2525250A%25252520%25252520%25252520%25252520is_active%25252520%2525253D%25252520models.BooleanField%252528default%2525253DTrue%252529%2525250A%25252520%25252520%25252520%25252520is_staff%25252520%2525253D%25252520models.BooleanField%252528default%2525253DFalse%252529%2525250A%25252520%25252520%25252520%25252520is_superuser%25252520%2525253D%25252520models.BooleanField%252528default%2525253DFalse%252529%2525250A%25252520%25252520%25252520%25252520USERNAME_FIELD%25252520%2525253D%25252520%25252522email%25252522%2525250A%25252520%25252520%25252520%25252520REQUIRED_FIELDS%25252520%2525253D%25252520%2525255B%2525255D%2525250A%2525250A%25252520%25252520%25252520%25252520%25252523%25252520Tells%25252520Django%25252520that%25252520the%25252520UserManager%25252520class%25252520defined%25252520above%25252520should%25252520manage%2525250A%25252520%25252520%25252520%25252520%25252523%25252520objects%25252520of%25252520this%25252520type.%2525250A%25252520%25252520%25252520%25252520objects%25252520%2525253D%25252520UserManager%252528%252529%2525250A%2525250A%25252520%25252520%25252520%25252520def%25252520__str__%252528self%252529%2525253A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520return%25252520self.email%2525250A%2525250A%25252520%25252520%25252520%25252520class%25252520Meta%2525253A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252522%25252522%25252522%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520to%25252520set%25252520table%25252520name%25252520in%25252520database%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252522%25252522%25252522%2525250A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520db_table%25252520%2525253D%25252520%25252522login%25252522&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/f3362e5183e09ae605ffbdde699da0fa/href">https://medium.com/media/f3362e5183e09ae605ffbdde699da0fa/href</a></iframe><p>Here we are inheriting from the <em>AbstractBaseUser</em> class to create our user.</p><p>Now we have set the <em>USERNAME_FIELD</em> to email to make the user login with their email. We can use any other atomic unique attribute for this purpose.</p><p>Some of the readers might have a doubt that why we didn’t create a password attribute. Actually it is already in the <em>AbstractBaseUser</em> which we inherited.</p><p>Next thing you will notice is</p><pre>objects = UserManager()</pre><p>so we need to create a class to manage the user entity. That class is the <em>UserManager</em> class.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528164%252C184%252C199%252C1%2529%26t%3Dvscode%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dclass%252520UserManager%2528BaseUserManager%2529%25253A%25250A%252520%252520%252520%252520def%252520create_user%2528self%25252C%252520email%25252C%252520password%25253DNone%2529%25253A%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252522%252522%252522%25250A%252520%252520%252520%252520%252520%252520%252520%252520Create%252520and%252520return%252520a%252520%252560User%252560%252520with%252520an%252520email%25252C%252520username%252520and%252520password.%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252522%252522%252522%25250A%252520%252520%252520%252520%252520%252520%252520%252520if%252520not%252520email%25253A%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520raise%252520ValueError%2528%252522Users%252520Must%252520Have%252520an%252520email%252520address%252522%2529%25250A%25250A%252520%252520%252520%252520%252520%252520%252520%252520user%252520%25253D%252520self.model%2528%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520email%25253Dself.normalize_email%2528email%2529%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520%2529%25250A%252520%252520%252520%252520%252520%252520%252520%252520user.set_password%2528password%2529%25250A%252520%252520%252520%252520%252520%252520%252520%252520user.save%2528using%25253Dself._db%2529%25250A%252520%252520%252520%252520%252520%252520%252520%252520return%252520user%25250A%25250A%252520%252520%252520%252520def%252520create_superuser%2528self%25252C%252520email%25252C%252520password%2529%25253A%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252522%252522%252522%25250A%252520%252520%252520%252520%252520%252520%252520%252520Create%252520and%252520return%252520a%252520%252560User%252560%252520with%252520superuser%252520%2528admin%2529%252520permissions.%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252522%252522%252522%25250A%252520%252520%252520%252520%252520%252520%252520%252520if%252520password%252520is%252520None%25253A%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520raise%252520TypeError%2528%252522Superusers%252520must%252520have%252520a%252520password.%252522%2529%25250A%25250A%252520%252520%252520%252520%252520%252520%252520%252520user%252520%25253D%252520self.create_user%2528email%25252C%252520password%2529%25250A%252520%252520%252520%252520%252520%252520%252520%252520user.is_superuser%252520%25253D%252520True%25250A%252520%252520%252520%252520%252520%252520%252520%252520user.is_staff%252520%25253D%252520True%25250A%252520%252520%252520%252520%252520%252520%252520%252520user.save%2528%2529%25250A%25250A%252520%252520%252520%252520%252520%252520%252520%252520return%252520user&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528164%25252C184%25252C199%25252C1%252529%26t%3Dvscode%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dclass%25252520UserManager%252528BaseUserManager%252529%2525253A%2525250A%25252520%25252520%25252520%25252520def%25252520create_user%252528self%2525252C%25252520email%2525252C%25252520password%2525253DNone%252529%2525253A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252522%25252522%25252522%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520Create%25252520and%25252520return%25252520a%25252520%25252560User%25252560%25252520with%25252520an%25252520email%2525252C%25252520username%25252520and%25252520password.%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252522%25252522%25252522%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520if%25252520not%25252520email%2525253A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520raise%25252520ValueError%252528%25252522Users%25252520Must%25252520Have%25252520an%25252520email%25252520address%25252522%252529%2525250A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520user%25252520%2525253D%25252520self.model%252528%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520email%2525253Dself.normalize_email%252528email%252529%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%252529%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520user.set_password%252528password%252529%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520user.save%252528using%2525253Dself._db%252529%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520return%25252520user%2525250A%2525250A%25252520%25252520%25252520%25252520def%25252520create_superuser%252528self%2525252C%25252520email%2525252C%25252520password%252529%2525253A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252522%25252522%25252522%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520Create%25252520and%25252520return%25252520a%25252520%25252560User%25252560%25252520with%25252520superuser%25252520%252528admin%252529%25252520permissions.%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252522%25252522%25252522%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520if%25252520password%25252520is%25252520None%2525253A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520raise%25252520TypeError%252528%25252522Superusers%25252520must%25252520have%25252520a%25252520password.%25252522%252529%2525250A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520user%25252520%2525253D%25252520self.create_user%252528email%2525252C%25252520password%252529%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520user.is_superuser%25252520%2525253D%25252520True%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520user.is_staff%25252520%2525253D%25252520True%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520user.save%252528%252529%2525250A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520return%25252520user&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/fd1e3737a61a428cc04ef56973802945/href">https://medium.com/media/fd1e3737a61a428cc04ef56973802945/href</a></iframe><p>The <em>UserManager</em> class contains methods to create a normal user and super user. We will be using this class instead of directly using the <em>User</em> class.</p><p>Next we will create a <em>UserProfile</em> class to store additional user data and configuration. This entity will have a one to one relationship with the User entity.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528164%252C184%252C199%252C1%2529%26t%3Dvscode%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dclass%252520UserProfile%2528models.Model%2529%25253A%25250A%252520%252520%252520%252520%252522%252522%252522%25250A%252520%252520%252520%252520to%252520store%252520all%252520other%252520attributes%252520associated%252520to%252520user%25250A%252520%252520%252520%252520%252522%252522%252522%25250A%25250A%252520%252520%252520%252520id%252520%25253D%252520models.UUIDField%2528primary_key%25253DTrue%25252C%252520default%25253Duuid.uuid4%25252C%252520editable%25253DFalse%2529%25250A%252520%252520%252520%252520user%252520%25253D%252520models.OneToOneField%2528User%25252C%252520on_delete%25253Dmodels.CASCADE%25252C%252520related_name%25253D%252522profile%252522%2529%25250A%252520%252520%252520%252520name%252520%25253D%252520models.CharField%2528max_length%25253D50%25252C%252520unique%25253DFalse%2529%25250A%25250A%252520%252520%252520%252520class%252520Meta%25253A%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252522%252522%252522%25250A%252520%252520%252520%252520%252520%252520%252520%252520to%252520set%252520table%252520name%252520in%252520database%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252522%252522%252522%25250A%25250A%252520%252520%252520%252520%252520%252520%252520%252520db_table%252520%25253D%252520%252522profile%252522&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528164%25252C184%25252C199%25252C1%252529%26t%3Dvscode%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dclass%25252520UserProfile%252528models.Model%252529%2525253A%2525250A%25252520%25252520%25252520%25252520%25252522%25252522%25252522%2525250A%25252520%25252520%25252520%25252520to%25252520store%25252520all%25252520other%25252520attributes%25252520associated%25252520to%25252520user%2525250A%25252520%25252520%25252520%25252520%25252522%25252522%25252522%2525250A%2525250A%25252520%25252520%25252520%25252520id%25252520%2525253D%25252520models.UUIDField%252528primary_key%2525253DTrue%2525252C%25252520default%2525253Duuid.uuid4%2525252C%25252520editable%2525253DFalse%252529%2525250A%25252520%25252520%25252520%25252520user%25252520%2525253D%25252520models.OneToOneField%252528User%2525252C%25252520on_delete%2525253Dmodels.CASCADE%2525252C%25252520related_name%2525253D%25252522profile%25252522%252529%2525250A%25252520%25252520%25252520%25252520name%25252520%2525253D%25252520models.CharField%252528max_length%2525253D50%2525252C%25252520unique%2525253DFalse%252529%2525250A%2525250A%25252520%25252520%25252520%25252520class%25252520Meta%2525253A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252522%25252522%25252522%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520to%25252520set%25252520table%25252520name%25252520in%25252520database%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252522%25252522%25252522%2525250A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520db_table%25252520%2525253D%25252520%25252522profile%25252522&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/511b03d9a67e486ab92f6b69dfe09155/href">https://medium.com/media/511b03d9a67e486ab92f6b69dfe09155/href</a></iframe><p>I have only added one additional attribute for the user, that is name. But you can add any number of attributes pertaining to the user in this class.</p><p>Here is the finished <em>models.py</em> file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd59cc22b6f3a6c5c4bfd3cab04ae293/href">https://medium.com/media/dd59cc22b6f3a6c5c4bfd3cab04ae293/href</a></iframe><h3><strong>Creating Serializers</strong></h3><p>Now that we have finished creating the user models, we can use the django ORM to create the corresponding tables in the database for us. for that open the terminal in the project folder and activate the python environment. Then run :</p><pre>python manage.py makemigrations<br>python manage.py migrate --run-sync-db</pre><p>After that we need to create serializers. Serializers are classes that <em>djangorestframework</em> users to serialize the entities defined in the <em>models.py</em> file to JSON and the reverse. These classes produce the final data that is given out to through the rest endpoint. So we need to create Serializer classes based on user actions.</p><p>So these are the user actions which we will create serializers for:</p><ol><li>User Registration</li><li>Viewing User (data in <em>User </em>entity)</li><li>User login</li><li>View User Details (data in <em>UserProfile</em> entity)</li></ol><p>So first create a file <em>serializers.py</em> in users directory. then create the serializer classes.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/13a4052258e2c90db26f7965aa5e4186/href">https://medium.com/media/13a4052258e2c90db26f7965aa5e4186/href</a></iframe><p>Now let me explain the the code.</p><p>the <em>djangorestframework</em> provides us with different types of serializer classes to use to create serializer. The way to make use of it is same as before; Inheritance. we here make use of the <em>ModelSerializer</em> class to serialize the django models we defined.</p><p><em>ModelSerializer</em> only requires us to provide which all fields we need it to serialize and what model to use in its inner Meta class. It has all the basic CRUD operations built-in. But if we need to do something extra we can override the methods of parent class like we did with <em>UserRegistrationSerializer</em> and <em>UserLoginSerializer</em>. One Thing you also would have noticed would be the <em>JWT_PAYLOAD_HANDLER</em> and <em>JWT_ENCODE_HANDLER</em>. Now these are the methods which is provided by the <em>djangorestframework-jwt</em> for us to create the JWT token for authentication. Here we use it during user login. After user credentials are verified with django’s authenticate function, we need to provide the user with the JWT token. For that we need to generate that first. the 2 JWT methods are used for that. The payload_handler actually encodes the <em>USERNAME</em> attribute of the <em>User</em> model in to the sub part of the JWT. The encoder creates the JWT from the payload and secret key which we are going to define later.</p><h3><strong>Creating Views</strong></h3><p>After creating the serializers, we can finally create the views. Views in <em>djangorestframework</em> are the actual REST API endpoints. Just like with serializers, <em>djangorestframework</em> provides us with builtin abstract classes to inherit from to create views.</p><p>To create our views we will open <em>views.py</em> file and add the following code to it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b50b38fb70916cbffc300e236601b89c/href">https://medium.com/media/b50b38fb70916cbffc300e236601b89c/href</a></iframe><p>In the views, there are separate classes defined by the <em>djangorestframework</em> which we can use. Different classes have different use-cases. I mainly used the <em>CreateAPIView</em> and <em>RetreiveAPIView</em>. If I wanted to update something, there is also <em>UpdateAPIView</em>. Now for these views, we need to define which serializer class we are going to use and permission classes. Permission class defines whether there should be any authentication needed for access to the API endpoint.</p><p>After setting the serializer, we can define how the view responds to various HTTP request by defining corresponding methods like get, post, put, delete etc… Within these methods, we can use the output of the serializer as is like I did for <em>UserView</em>, or we can create a custom response in the format we desire like the other endpoints.</p><p>In the case of <em>UserProfileView</em>, we used authentication there. So only logged-in users are able to access it. There the <em>permission_classes</em> is set to <em>IsAuthenticated</em>. Then we also have to specify what kind of authentication it is to <em>djangorestframework</em> by defining <em>authentication_class</em>.</p><h3><strong>Wiring Views to URLs</strong></h3><p>Now that we have finished defining all the views, we can wire them to corresponding urls through a <em>urls.py</em> file.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528164%252C184%252C199%252C1%2529%26t%3Dvscode%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dfrom%252520django.urls%252520import%252520path%25250Afrom%252520.views%252520import%252520%2528%25250A%252520%252520%252520%252520UserRegistrationView%25252C%25250A%252520%252520%252520%252520UserLoginView%25252C%25250A%252520%252520%252520%252520UserProfileView%25252C%25250A%2529%25250A%25250Aurlpatterns%252520%25253D%252520%25255B%25250A%252520%252520%252520%252520path%2528%252522signup%252522%25252C%252520UserRegistrationView.as_view%2528%2529%2529%25252C%25250A%252520%252520%252520%252520path%2528%252522signin%252522%25252C%252520UserLoginView.as_view%2528%2529%2529%25252C%25250A%252520%252520%252520%252520path%2528%252522profile%252522%25252C%252520UserProfileView.as_view%2528%2529%2529%25252C%25250A%25255D%25250A&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528164%25252C184%25252C199%25252C1%252529%26t%3Dvscode%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dfrom%25252520django.urls%25252520import%25252520path%2525250Afrom%25252520.views%25252520import%25252520%252528%2525250A%25252520%25252520%25252520%25252520UserRegistrationView%2525252C%2525250A%25252520%25252520%25252520%25252520UserLoginView%2525252C%2525250A%25252520%25252520%25252520%25252520UserProfileView%2525252C%2525250A%252529%2525250A%2525250Aurlpatterns%25252520%2525253D%25252520%2525255B%2525250A%25252520%25252520%25252520%25252520path%252528%25252522signup%25252522%2525252C%25252520UserRegistrationView.as_view%252528%252529%252529%2525252C%2525250A%25252520%25252520%25252520%25252520path%252528%25252522signin%25252522%2525252C%25252520UserLoginView.as_view%252528%252529%252529%2525252C%2525250A%25252520%25252520%25252520%25252520path%252528%25252522profile%25252522%2525252C%25252520UserProfileView.as_view%252528%252529%252529%2525252C%2525250A%2525255D%2525250A&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/622340e435c77755a6a97ed1eefd0553/href">https://medium.com/media/622340e435c77755a6a97ed1eefd0553/href</a></iframe><p>Once that is done, we can go and create our main <em>urls.py</em> file in <em>backend</em> directory</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528164%252C184%252C199%252C1%2529%26t%3Dvscode%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dfrom%252520django.contrib%252520import%252520admin%25250Afrom%252520django.urls%252520import%252520path%25252C%252520include%25250Afrom%252520rest_framework_jwt.views%252520import%252520refresh_jwt_token%25250A%25250Aurlpatterns%252520%25253D%252520%25255B%25250A%252520%252520%252520%252520path%2528%252522admin%25252F%252522%25252C%252520admin.site.urls%2529%25252C%25250A%252520%252520%252520%252520path%2528%252522auth%25252F%252522%25252C%252520include%2528%252522users.urls%252522%2529%2529%25252C%25250A%252520%252520%252520%252520path%2528%252522auth%25252Ftoken-refresh%25252F%252522%25252C%252520refresh_jwt_token%2529%25252C%25250A%25255D%25250A&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528164%25252C184%25252C199%25252C1%252529%26t%3Dvscode%26wt%3Dnone%26l%3Dauto%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dfrom%25252520django.contrib%25252520import%25252520admin%2525250Afrom%25252520django.urls%25252520import%25252520path%2525252C%25252520include%2525250Afrom%25252520rest_framework_jwt.views%25252520import%25252520refresh_jwt_token%2525250A%2525250Aurlpatterns%25252520%2525253D%25252520%2525255B%2525250A%25252520%25252520%25252520%25252520path%252528%25252522admin%2525252F%25252522%2525252C%25252520admin.site.urls%252529%2525252C%2525250A%25252520%25252520%25252520%25252520path%252528%25252522auth%2525252F%25252522%2525252C%25252520include%252528%25252522users.urls%25252522%252529%252529%2525252C%2525250A%25252520%25252520%25252520%25252520path%252528%25252522auth%2525252Ftoken-refresh%2525252F%25252522%2525252C%25252520refresh_jwt_token%252529%2525252C%2525250A%2525255D%2525250A&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/82a64d6a75586429567dbd6674641449/href">https://medium.com/media/82a64d6a75586429567dbd6674641449/href</a></iframe><p>Here you can see we are also providing a built in API endpoint for token refresh. It is used to create new token for a logged in user when when existing token is about to be expired.</p><h3><strong>Final Configurations</strong></h3><p>Now for the final touches to be added to <em>settings.py</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d4070b8042cdbebade1d2250e9ed7b43/href">https://medium.com/media/d4070b8042cdbebade1d2250e9ed7b43/href</a></iframe><p>I have placed the code for whole <em>settings.py</em> file. So if you scroll down in its content, you can see the JWT configuration added.</p><p>Now with that finished, the basic code for the project is complete. So you can start running it by running:</p><pre>python manage.py runserver</pre><p>You can test out the API using postman.</p><h3><strong>Templating the Project</strong></h3><p>Now, we created the basic configuration, we will see how to<strong> not do this every single time.</strong></p><p>For this we use a tool called <em>cookiecutter</em>. We can easily install this via pip.</p><pre>pip install cookiecutter</pre><p>After that it is just a matter of templating. Cookiecutter uses <em>jinja2</em> templating format. Here I am just templating the project name, with</p><pre>{{cookiecutter.project_name}}</pre><p>so where ever you have the project name in your code(for me the name was “backend”) replace it with the template variable.</p><p>For django, project name will be also in some of the auto generated files such as:</p><ol><li>manage.py</li><li>settings.py</li><li>asgi.py</li><li>wsgi.py</li></ol><p>So we have to replace it in these files. Once that is complete create a outer directory over the project directory and rename the project directory as:</p><pre>{{cookiecutter.project_slug}}</pre><p>then create a JSON file called <em>cookiecutter.json</em> with the below contents.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528164%252C184%252C199%252C1%2529%26t%3Dvscode%26wt%3Dnone%26l%3Dapplication%252Fjson%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%25257B%25250A%252520%252520%252520%252520%252522author_name%252522%25253A%252520%252522author%252520name%252522%25252C%25250A%252520%252520%252520%252520%252522project_name%252522%25253A%252520%252522project_name%252522%25252C%25250A%252520%252520%252520%252520%252522project_slug%252522%25253A%252520%252522%25257B%25257B%252520cookiecutter.project_name.lower%2528%2529.replace%2528%2527%252520%2527%25252C%252520%2527_%2527%2529.replace%2528%2527-%2527%25252C%252520%2527_%2527%2529%252520%25257D%25257D%252522%25250A%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528164%25252C184%25252C199%25252C1%252529%26t%3Dvscode%26wt%3Dnone%26l%3Dapplication%25252Fjson%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%2525257B%2525250A%25252520%25252520%25252520%25252520%25252522author_name%25252522%2525253A%25252520%25252522author%25252520name%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252522project_name%25252522%2525253A%25252520%25252522project_name%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252522project_slug%25252522%2525253A%25252520%25252522%2525257B%2525257B%25252520cookiecutter.project_name.lower%252528%252529.replace%252528%252527%25252520%252527%2525252C%25252520%252527_%252527%252529.replace%252528%252527-%252527%2525252C%25252520%252527_%252527%252529%25252520%2525257D%2525257D%25252522%2525250A%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/915ad8552d7e1d53279e4ee526284025/href">https://medium.com/media/915ad8552d7e1d53279e4ee526284025/href</a></iframe><p>After that make this into a git repository and push it to github.</p><p>Now whenever you need to create a project with <em>djangorestframework</em>, you can instantly use this to have basic things already configured. To use this, open a terminal where you want to create the project and run:</p><pre>cookiecutter <a href="https://github.com/ebin7joseph/JWT-Token-Auth-Django-Seed.git">https://github.com/ebin7joseph/JWT-Token-Auth-Django-Seed.git</a></pre><p>Here I have place my github repository url. You can use your own instead. I have the whole project in <a href="https://github.com/ebin7joseph/JWT-Token-Auth-Django-Seed.git">github</a> if any of you are interested in reusing this template for your own project.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8687494509d8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>