SyntaxStudy
Sign Up
Django User Registration and Custom User Models
Django Beginner 1 min read

User Registration and Custom User Models

Django's built-in authentication URLs do not include a registration view, so you need to create one yourself. The UserCreationForm from django.contrib.auth.forms provides a ready-made form with username, password, and password confirmation fields. You can subclass it to add extra fields like email. After saving the form you typically call login() to automatically log in the new user. Django strongly recommends defining a custom User model at the start of every project, even if you don't need to customise it immediately. Setting AUTH_USER_MODEL = 'accounts.CustomUser' in settings.py before running any migrations future-proofs your project — changing the User model later is very difficult once migrations exist. The easiest approach is to create a model that extends AbstractUser and leaves all fields unchanged initially. AbstractUser provides the full built-in User functionality (username, email, password, groups, permissions) and lets you add extra fields. AbstractBaseUser gives you even more control over the authentication mechanism itself — useful when you want email-only login without a username. Whichever base class you choose, you should also define a custom manager and reference your custom model via get_user_model() rather than importing User directly.
Example
# accounts/models.py — custom user model
from django.contrib.auth.models import AbstractUser
from django.db import models


class CustomUser(AbstractUser):
    bio     = models.TextField(blank=True)
    website = models.URLField(blank=True)
    avatar  = models.ImageField(upload_to='avatars/', blank=True, null=True)

    def __str__(self):
        return self.username


# mysite/settings.py
AUTH_USER_MODEL = 'accounts.CustomUser'

# accounts/forms.py — registration form
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import get_user_model

User = get_user_model()


class RegisterForm(UserCreationForm):
    email = forms.EmailField(required=True)

    class Meta(UserCreationForm.Meta):
        model  = User
        fields = ['username', 'email', 'password1', 'password2']


# accounts/views.py — registration view
from django.shortcuts import render, redirect
from django.contrib.auth import login
from .forms import RegisterForm


def register(request):
    if request.method == 'POST':
        form = RegisterForm(request.POST)
        if form.is_valid():
            user = form.save()
            login(request, user)
            return redirect('blog:post-list')
    else:
        form = RegisterForm()
    return render(request, 'registration/register.html', {'form': form})