Set-up Authentication System in Django in just 10 minutes
Posted: 03 October 2017 | Post By: Jai Singhal | |
13 min read |
Authentication is something which needs to be implemented on each and every website. And with Django, it is very simple and easy to implement because we don’t need to make the Authentication system from scratch, it is already made by the Django, we need to simply use it.
In this tutorial, we will be discussing that how to implement the login, logout, and register views in a brand new accounts app.
Initial Set Up
Let’s create a new django-app named accounts to your Project.
Make an entry of that app to Installed Apps in the Django settings.
Create two new .py files to your accounts app that is forms.py and urls.py
Then this would be your following App structure.
Create some main URLs to your main project. Note that I have included the accounts urls of accounts app.
Note: Change the DIRS OF Templates settings in your Django settings.
Let’s create a base template so that we can you use that for Template Inheritance. If you have already made a base file Ignore this step.
Create a home page or a landing page with the navbar. Again repeating if you have your own navbar and home page, you don’t need to change it, I am just discussing for the sake of completion of the tutorial.
Let’s begin with the login system. Like I have said before that you don’t need to create from scratch. Like we don’t need to create the database for the Login system like username and password fields. We Just need to create a Django form of fields including the username and password. We can also create new additional fields (if required) in Login.
I have created a Django form for the Login View with fields username and password and also a clean method which raises some common Validation Errors like Incorrect Password, User does not Exist, User Not Active etc. You can create more Validation Errors according to your need. Also, I have added classes in fields of username and password.
Now after creating a form, let’s create a Django View which accepts the request and render a request to templates. I have implemented the Function Bases View which handles both GET and POST request methods.
Create urls for the respective view, like in our case that is login_view
templates/ accounts/ form.html
Now I have created a template for the login view, you can change according to your need. Note that I have inherited the base.html, which is discussed above.
If you have followed the exact tutorial, you will have this kind of output for Login Page
After creating the Login View, let’s create the register view.
Starting with forms, let’s create a Register Form with a class named UsersRegistrationForm which is somewhat similar to that of UsersLoginForm. So in this form, we have fields of username, password, email, and confirm_email. Aand along with this a clean method which is some standard validation errors.
Now let’s create the view for the register, This is view is Function Bases View which is for handling both GET and POST request.
creating url for the register_view in the accounts urls.
Now let’s create our last view i.e., logout view. This is very simple view and very straightforward.
In my accounts views, I have created a new view named logout_view, which simply logouts and redirect to home.
Writing the view url to accounts urls.
I hope that you have successfully implemented the Authentication System to your Django Project. However, if you find any problem in any of the steps discussed above, you can check our Github Repository from here
And also feel free to comment down below, if you have any problem regarding this tutorial and please share your experience with the DjangoPy