๐Ÿ“ Django

Authentication in Django

P
Author
PyLand Team
๐Ÿ“…
Published
30.06.2026
โฑ๏ธ
Reading time
1 min
๐Ÿ‘๏ธ
Views
285
๐ŸŒฟ
Level
Medium

Django includes a complete authentication system: the User model, login/logout, and permissions.

User Model

from django.contrib.auth.models import User

# Creating
user = User.objects.create_user(
    username='alice',
    email='alice@example.com',
    password='secret123'
)

# Retrieving
user = User.objects.get(username='alice')
print(user.username, user.email, user.is_staff, user.is_superuser)

Connecting Authentication URLs

# mysite/urls.py
from django.contrib.auth import views as auth_views

urlpatterns = [
    path('accounts/', include('django.contrib.auth.urls')),
    # Provides:
    # /accounts/login/          โ†’ auth_views.LoginView
    # /accounts/logout/         โ†’ auth_views.LogoutView
    # /accounts/password_change/ and more
]

Login Page

A registration/login.html template is required:

<form method="post">
  {% csrf_token %}
  {{ form.as_p }}
  <button type="submit">Log in</button>
</form>

In settings.py:

LOGIN_REDIRECT_URL = '/'        # redirect destination after login
LOGOUT_REDIRECT_URL = '/accounts/login/'
LOGIN_URL = '/accounts/login/'  # redirect destination when not authenticated

Registration

from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render, redirect

def register(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            user = form.save()
            login(request, user)
            return redirect('home')
    else:
        form = UserCreationForm()
    return render(request, 'registration/register.html', {'form': form})

login / logout in Code

from django.contrib.auth import login, logout, authenticate

# Authenticate and log in
user = authenticate(request, username='alice', password='secret123')
if user:
    login(request, user)

# Log out
logout(request)

Checking in a Template

{% if user.is_authenticated %}
    <p>Hello, {{ user.username }}!</p>
    <a href="{% url 'logout' %}">Log out</a>
{% else %}
    <a href="{% url 'login' %}">Log in</a>
{% endif %}

Your reaction to the article

๐Ÿ’ฌ Comments (0)

๐Ÿ” Sign in to leave a comment
๐Ÿšช Login
๐Ÿ’ญ

No comments yet

Be the first to share your opinion about this article!

๐Ÿ”— Similar

Similar articles

Continue learning with these materials

๐Ÿ“

Authentication in DRF

DRF supports several authentication methods. The right choice depends on the client type, threat model,...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 283
๐Ÿ“

Token Authentication in DRF

Covered topics: Setup, Endpoint for Obtaining a Token, Using the Token, Creating a Token for...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 258
๐Ÿ“

Search in Django

You can start with simple Django ORM filters and move to PostgreSQL features when you...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 280
๐ŸŽ“ Continue learning

Courses that cover this material

Visit the course to apply this material in practice.

Django RequestLab: from HTTP to secure production Open course curriculum