Django 2.1 and Python 3 User Authentication System — #1(Using the Inbuilt Django User model)

Abedy Ng'ang'a
6 min readDec 25, 2018

Before I begin, there are three known approaches to Authentication using the Django Framework. In this article we are going to cover the first and most basic approach, the approach where you use the User model that comes with Django by default. I’ll be publishing articles on the other two approaches later. Now if you’re starting on Django or you want to start working with Django, then please read this first. Trust me, you won’t regret it. I’ll also be posting articles on other famous scenario problems which you can easily solve with the Django framework later on.

So now let’s get Started.

Django, as you all know is a Python — Web application Framework and so you’re going to need some basic knowledge on Python Syntax. I’ll begin by assuming you have your virtual environment all set up. Again if you don’t know about virtual environments, I got you sorted here.

We’ll begin with the commands. I’m using Kali Linux, so I have my terminal open. If you’re on windows, the CMD will just do the task. But ensure your virtual environment is first activated. First we’ll install django

pip install django

The next step will be to start a project

django-admin startproject my_auth_project

This will create a project “my_auth_project” with the whole structure for us. To see the structure, we’ll

cd my_auth_project/

and then you can run the “ls” or “tree” command to see the structure. Below is my output.

So as we can see, we already have one directory created for us. I’m not going to talk much about static files and django templates but you can read about them here. I prefer setting them in the root of my project and as such we’re going to edit our settings.py file as follows.

First we’ll change the “TEMPLATES” list to the following

and then we’ll add this to the base of our settings.py file

Then we’ll now add two folders to the root of our project. “static” and “templates

Now its time to test whether everything works okay, and to do that we’ll run

python manage.py runserver

The server should now start running and you should see an output similar to this

The next thing is to open your browser and visit the address `127.0.0.1:8000` . You should see an output similar to this

Now it’s time to start setting things up. First we’ll need three things, a login page, a sign up page and a home page for redirecting once the user is logged in. So to do this, we’ll need to create a “users” app. Again if you don’t know what apps are in django, then read the docs here. To do this, run

python manage.py startapp users

Now at this point I need to specify that I’m using python3. So if you’re using Linux then you might want to specify python3 on your terminal just in case you’re not using a virtual environment.

The command above will create a new directory called “users”.

As we can see, the users app has been created with its corresponding files. Now we only need to add one file in the users app called urls.py, so make sure you do that. Then in our settings.py file, in the ‘INSTALLED_APPS’ list, you’ll need to add the users app configurations by adding the following line of code inside the list

‘users.apps.UsersConfig’

Now remember its a list so don’t forget to add the commas appropriately.

Next we’ll go to the ‘urls.py’ file in our ‘my_auth_project’ directory and edit it to this.

and then the ‘urls.py’ file in our users directory to

and now the intro method in the ‘views.py’ file

Now on your browser, you should see this, and always remember to ensure that the server is running.

Now it’s time to start working on the registration. But before that, we need to enable our database. To do that we’re going to do

python manage.py migrate

and then we’ll need to create a superuser by

python manage.py createsuperuser

If you don’t know what superusers and migrations are all about then please read this. Super users help us to login to the admin page, using the ‘admin/’ route.

Well first create the sign up functionality, and to do that we’ll create a method in the ‘views.py’ file as follows

Next well do two things, we’ll create that template “users/register.html”, then well add the url pattern for that register functionality. But before that, I need to tell you of the coolest functionality I know in Django. And that is jinja templating. Jinja enables you to dynamically add/update content, as well as check conditions within the same HTML page. Isn’t that cool?

So we’ll create that template. And to do that, we’ll go to the templates directory we created in the root of our project and in that folder we’ll create another directory called ‘users’. In that users folder we’ll create the ‘register.htmlfile, and in that file we’ll include the following content

But wait a minute 😒, what the hell is happening? From what I’m seeing, we’re extending from a file called ‘layout_auth.html. And that’s the cool thing here, we can create create one layout file and dynamically change the content by creating sub files 😃. So now let’s create that layout file. But before that we need to install something called ‘crispy forms’. What crispy forms do is, they make our forms more presentable. To do that we’ll run

pip install django-crispy-forms

on our terminal and then in the base of our settings file include the line

and in the ‘INSTALLED APPS’ list include the following item.

'crispy_forms',

Also note that I’m using bootstrap classes. But where are they included? Wait for it…

Now there’s this thing called static. What is it? The ‘static’ directory we created in the root of our project is where we store our CSS, JS, Image files and all other sorts of external files we need in our project.

So in my case I’ve downloaded bootstrap 4 and stored the bootstrap folder in the static directory. Then I’ve created a main_auth.css file.which includes the following content

Now let’s create the url pattern for the register, login and logout route. So we’ll update our ‘urls.py’ — users directory to this

Now we need to create those two templates, ‘login.html’ and ‘logout.html’ inside the ‘templates/users’ directory

So ‘login.htmlwill be as follows

and ‘logout.html’ as follows

Now we only have one thing remaining. The home function for redirecting after login/sign up. To do that we’ll do three things, we’ll add a url pattern, create a home.htmltemplate and a views home function.

The url pattern will be as follows in the ‘urls.py’ — users directory.

path(‘home/’, as_views.home, name=’home’),

The ‘home.html’ template will be as follows.

and the home function in the ‘views.py’ file will be as follows.

Finally we’ll need to add these few lines to the base of our settings.py’ file.

And just like that we’re done. I hope you did learn something, and I hope you enjoyed reading too. In case you have any queries or you need any clarifications feel free to contact me. I wish you a merry Christmas.

--

--