Introduction to Django's Auth System
What django.contrib.auth Provides
Django's auth app provides a User model, login/logout views, password hashing, permissions, and groups out of the box. It is listed in INSTALLED_APPS in every new project.
Note: You rarely need to write authentication logic yourself — Django already solved it.
Example: What django.contrib.auth Provides
These are the default apps Django adds to every new project; 'django.contrib.auth' is what powers users, permissions and login.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
request.user in Every View
AuthenticationMiddleware attaches a user object to every incoming request. If nobody is logged in, request.user is an AnonymousUser instead of None.
Warning: Removing AuthenticationMiddleware from MIDDLEWARE breaks request.user everywhere.
Example: request.user in Every View
is_authenticated is False for AnonymousUser and True for a real logged-in User, so this check never crashes.
from django.http import HttpResponse
def whoami(request):
if request.user.is_authenticated:
return HttpResponse(f'Hello, {request.user.username}')
return HttpResponse('Hello, anonymous visitor')
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
The MIDDLEWARE Setting
SessionMiddleware and AuthenticationMiddleware must both be present, in that order, for logins to work: sessions store the login, and auth middleware reads the session.
Note: Order matters — AuthenticationMiddleware depends on SessionMiddleware running first.
Example: The MIDDLEWARE Setting
SessionMiddleware must come before AuthenticationMiddleware because auth reads the logged-in user id out of the session.
MIDDLEWARE = [
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
]
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
- Assuming django.contrib.auth needs to be installed separately — it ships with every new Django project by default.
- Writing custom login/password-checking code instead of using the battle-tested auth system already provided.
- Forgetting that AuthenticationMiddleware must stay in MIDDLEWARE for request.user to be available in views.
- django.contrib.auth is Django's built-in app for users, passwords, sessions, and permissions.
- It is enabled by default via INSTALLED_APPS and AuthenticationMiddleware in settings.py.
- Every request gets a request.user object — either a real logged-in User or an AnonymousUser.
- The system handles password hashing, login sessions, and permission checks so you don't build them from scratch.
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first:
- Introduction to Django's Auth System
- The User Model Overview
- Setting Up a Login View
- Logout Functionality
- User Registration Form
- The login_required Decorator
- The permission_required Decorator
- Django Groups and Permissions
- Password Hashing in Django
- Session Authentication Basics
- Introduction to Custom User Models
- Sending Emails with Django