Task
Implement a user authentication system. For this purpose, a registration form and login form are created on the website timthewebmaster.com.
The registration form must contain the following elements:
- user name ( required )
- user email ( required )
- password ( required )
- repeat password ( required )
- field about the user himself
- user avatar
- registration buttons
The login form should consist of only two fields:
- user name
- password
There should also be a button that would allow you to exit the authenticated user mode.
Solution
Including jQuery to pages templates
For solving such case I need to include one JS library jQuery . This is a popular and simple library. And I need to change security settings of website developed on django. That means change a little bit of settings.py
Setup django for register new users
If be honest it is a not necessary step. But for security sake and for to be sure none of the hackers couldn’t broke up database of server and gain access to credentials of users.
In a file settings.py just add:
In the app that main goal to register an user, open urls.py and add 2 more paths.
Let’s create ajax request for user registration
Now it is a time of JS-script with POST request.
If successful, that is, if all checks are passed, such as password length or existing email, I will give a feedback and redirect to the login page.
If unsuccessful, I will notify the user about this too.
Write down function to register new user to mysql database
In the pre-configuration chapter, we imported the signup_verify function in urls.py, let's define it
This is how I identify form “errors” in Django.
Gather all together in registration page
Now, for everything to work, I need to create a template that would load the appropriate script and styles, use the token.
Here is the form code
And at the very bottom before the body tag, insert a script written in advance
Setup urls.py for user authentication
Need to add 2 additional paths and import corresponding functions
Writing down ajax request for login page with email
The principle is the same as during registration. We indicate the request type, request url and data that we want to transfer. And in addition, of course, feedback for the user. So that he knows about the mistakes he made when filling out the form. All this is in a separate file login.js
Writing down function for managing user login
Here are ready-made functions for processing and checking data entered by the user
User exit function. I'm simply clearing the current user session on my site. And I redirect to the site login page.
Template for authentication on website
And of course my template for logging into the site
And of course, don’t forget to insert the script into the template. The login.js and jQuery library we wrote
In Conclusion
In this case, I demonstrated two main forms for a user authentication system. Registration form and login form. Not the most difficult thing, especially if you know what you're doing.
I want to make a reservation right away: I did not add a captcha or email verification here for one simple reason. This is authentication on my site and I didn't need it. In my activities, it is not the number of users that is important, but their quality. That is, if a person wants to contact me, he will contact me, if not, well, that’s his business.
Additional content
Github repository
The repository that I use to develop this site
Specifically in this case, you will be interested in the following files:
- Script written in jQuery for registration
- Script written in jQuery for login page
- Setting up ulrs
- Directly the page rendering function, signup login logout
- And of course the template that django uses to render the registration form
- And of course the template that django uses to render the login form
Content that I’ve used to learn
Do not forget to share, like and leave a comment :)
Comments
(0)
Send
It's empty now. Be the first (o゚v゚)ノ
Other
Similar articles
Implementation of authentication system on django
30.10.2023
05.10.2025
590
0
0
0
0
Let me make a reservation right away that the authentication system that you and I will write is not based on the built-in Django application, django.contrib.auth. This will be a …
How to implement yourown API using Django rest framework
24.02.2025
08.03.2026
857
1
0
0
0
This article describes the process of setting up and adding a REST framework to a site written in Django. It is added in order to build an API for access …
Used termins
- Website builder ⟶ It is an app or web service, with a collection of ready for use templates and tools, for constructing a website.
- Django model ⟶ Is a database manager used in the Django framework. Implemented via classes and inheritance in python.
- Django framework ⟶ Is a high-level, open-source web framework for building web applications using the Python programming language. It follows the model-view-template (MVT) architectural pattern and is designed to facilitate rapid development while promoting clean, pragmatic design.
- Django middleware ⟶ Is a way to process requests globally before they reach the view or after the view has processed them. Middleware is a framework of hooks into Django's request/response processing and is used for several purposes.
- Website ⟶ Is defined as a collection of related web pages that are typically identified by a common domain name and published on at least one web server. Websites can serve various purposes and can include anything from personal blogs to business sites, e-commerce platforms, or informational resources.
Related questions
- Why did you choose django over website builders ? Quite a difficult question. I guess I like to understand and delve into complex things, although it seems that it’s not worth it. It's a similar story with game development. Instead of making games using game engines, I made my own, **DI**. Well, I'm just not looking for easy ways ;)
- I can’t stand Django template language. Do I have to use it? I think this template engine is the best thing ever, but I know that choosing a template language runs close to religion. There’s nothing about Django that requires using the template language, so if you’re attached to Jinja2, Mako, or whatever it's ok.