Django’s built - in User
model is at the heart of its authentication system. It contains fields like username
, password
, email
, etc. You can use the default User
model or create a custom user model if you need to add extra fields or change the authentication behavior.
Authentication backends are classes that Django uses to authenticate users. The default backend is ModelBackend
, which authenticates users against the User
model in the database. You can also use other backends like RemoteUserBackend
for single - sign - on with a remote server.
Django uses sessions to keep track of authenticated users across requests. When a user logs in, Django creates a session and stores a unique session ID in a cookie on the user’s browser. On subsequent requests, Django uses this session ID to identify the user.
The most basic usage scenario is allowing users to log in and log out of the application. This is typically done on a login page where users enter their credentials, and a logout link is provided on the application’s interface.
You may want to restrict access to certain views or parts of the application to authenticated users only. For example, a user dashboard should only be accessible to logged - in users.
Users may forget their passwords, so you need to provide a password reset mechanism. This usually involves sending an email with a password reset link to the user’s registered email address.
First, create a new Django project and an app:
django - admin startproject auth_project
cd auth_project
python manage.py startapp auth_app
Add the auth_app
to the INSTALLED_APPS
list in auth_project/settings.py
:
# auth_project/settings.py
INSTALLED_APPS = [
#...
'auth_app',
]
In auth_app/views.py
, create views for login and logout:
# auth_app/views.py
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login, logout
from django.contrib import messages
def login_view(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(request, username = username, password = password)
if user is not None:
login(request, user)
return redirect('home')
else:
messages.error(request, 'Invalid username or password.')
return render(request, 'login.html')
def logout_view(request):
logout(request)
return redirect('login')
Create a templates
directory in the auth_app
and inside it, create login.html
:
<!-- auth_app/templates/login.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<title>Login</title>
</head>
<body>
<h1>Login</h1>
{% if messages %}
{% for message in messages %}
<p>{{ message }}</p>
{% endfor %}
{% endif %}
<form method="post">
{% csrf_token %}
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br>
<button type="submit">Login</button>
</form>
</body>
</html>
In auth_app/urls.py
, create URL patterns for the views:
# auth_app/urls.py
from django.urls import path
from .views import login_view, logout_view
urlpatterns = [
path('login/', login_view, name='login'),
path('logout/', logout_view, name='logout'),
]
And include these URLs in the project’s urls.py
:
# auth_project/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('auth_app.urls')),
]
If you don’t enforce strong password policies, users may choose weak passwords, which can lead to security vulnerabilities. Django provides password validators that you can configure to enforce strong passwords.
When authenticating users, you need to handle errors gracefully. For example, if the user enters incorrect credentials, you should display a meaningful error message instead of crashing the application.
If you don’t set proper session expiration times or use insecure cookie settings, sessions can be hijacked. Make sure to set appropriate SESSION_COOKIE_AGE
and SESSION_COOKIE_SECURE
settings in your settings.py
file.
Django provides built - in forms for authentication, such as AuthenticationForm
and PasswordResetForm
. Using these forms can save you a lot of time and ensure that the forms are secure.
Configure password validators in your settings.py
file to enforce strong passwords. For example:
# auth_project/settings.py
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
'OPTIONS': {
'min_length': 8,
}
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
Set appropriate session settings in your settings.py
file:
# auth_project/settings.py
SESSION_COOKIE_AGE = 1209600 # 2 weeks in seconds
SESSION_COOKIE_SECURE = True # Use only in HTTPS
Implementing authentication in Django correctly is crucial for the security and usability of your web application. By understanding the core concepts, typical usage scenarios, and avoiding common pitfalls, you can build a robust authentication system. Following best practices like using Django’s built - in forms, implementing password policies, and securing your sessions will help you create a more secure and reliable application.