Built-in Authentication & User Customization
Django ships with a complete user authentication system. Extending it with a custom User model is recommended for all production projects.
1 Custom User Model Setup
Python — models.py
from django.contrib.auth.models import AbstractUser
from django.db import models
class CustomUser(AbstractUser):
bio = models.TextField(max_length=500, blank=True)
birth_date = models.DateField(null=True, blank=True)
avatar = models.ImageField(upload_to="avatars/", null=True, blank=True)
# Configure this in settings.py:
# AUTH_USER_MODEL = "users.CustomUser"
2 Auth Views & Decorators
Python — views.py
from django.contrib.auth.decorators import login_required
from django.shortcuts import render
@login_required(login_url="login")
def dashboard_view(request):
return render(request, "dashboard.html", {"user": request.user})
3 Code Challenge
Challenge: Write a custom middleware that redirects users who have not verified their email addresses to a specific registration page.