Django Auth System
Learn how to manage users in Django with ease - tutorial for beginners.
Being a "batteries-included" framework, Django comes with a powerful authentication/authorization system that we can use and extend in our projects with ease. For those that start from nothing, feel free to access the resources mentioned below and come back here once the content is understood:
How to install Django - simple, tested steps to install Django
Django for beginners - a comprehensive tutorial that covers the basics
Topics covered in this tutorial
User
the table where the information is savedHow to create a new user using the Django CLI
How to create a
superuser
How to
update the password
- Django CLICreate a user using forms
User Model
As mentioned in the official Django documentation, the User model represents the core entity used to save and manage authentication. The fields managed by the User model can be found below:
Field
Sample Value
Information
username
test
Mandatory Field
password
Super_S3cret
optional for inactive users
test@appseed.us
optional
fist_name
John
optional
last_name
Doe
optional
Probably the most simple way to create a new user in Django is to use the CLI (Django shell). In case you don't have already a Django project, feel free to clone an open-source sample provided by the AppSeed Team to explain many Django concepts:
Create a virtual environment - Linux-based systems
For Windows system, the syntax is different:
Install Django
Create Users - Django CLI
The user creation process using the terminal is usually related to the superuser
that allows us to access the admin
section. For newcomers, the admin section manages the registered users, groups defined in our project.
Create the
superuser
in Django
Once the superuser
admin is created we can access the admin
section and interact with all models registered by our project. Let's explore the users using the Django CLI:
We can see the new admin saved a few seconds ago.
Using the CLI we can explore all properties and of course update fields.
Create a new (common) user
As we can see, new users can be created with ease using the create-user
helper provided by User
model - Let's check again all registered users:
Create Users via UI
Using the console to create and manage users might be fun but might be also useful in our projects to allow users to register themselves using a public web page. To do this, we need a simple page where the form is exposed and a backend to handle the information sent to the user.
Create the SignUp Form
Create the controller
The page that shows the form and invite the user to register
The user registration mechanism
The User sees the registration page
The User inputs all fields
The form is submitted and the controller receives all information (username, password)
If the form is valid, the form is
saved
and the user is createdA confirmation message is returned to the user
The above sample uses a form to create the user but we can update the code to use the create-user
method as well:
Authenticated User
Django hooks the authenticated in the request
object and we can check if a request is done by an authenticated user or not by calling a helper. The same check is available in views.
Check user is authenticated in controller -
is_authenticated
(boolean) property
Check user is authenticated in views
Logout Users
The logout
helper is provided by Django.contrib.auth
middleware package:
If the user is authenticated all session information will be deleted. If the user is not authenticated, the logout
helper will run without returning errors or exceptions.
Thanks for reading! For more topics, feel free to contact Appseed.
Resources
Read more about Django (official docs)
Start fast a new project using development-ready Django Starters
Last updated
Was this helpful?