User Registration

View saved

Goal for this part

Let visitors create accounts. You will add a users app, a registration view with Django’s user creation form (or a small custom subclass), Bootstrap-friendly crispy fields, and success messages.

After this snapshot, a new user can register and see feedback—without needing the admin to create every account by hand.

What you should already know

You should be comfortable with views, templates, and the built-in User model used as post authors. You do not need prior experience with Django forms.

Forms in Django handle rendering, validation, and cleaning. That keeps password rules and duplicate usernames out of ad-hoc if-statements.

Concepts: ModelForm, messages, and crispy forms

UserCreationForm already knows how to create a user with a hashed password. A thin subclass can add an email field and validation. On valid POST, save the user and redirect—Post/Redirect/Get keeps refresh from double-submitting.

Django’s messages framework stores one-time alerts in the session. Crispy Forms helps render fields with consistent Bootstrap classes so the form matches Campus Press styling.

from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django import forms

class UserRegisterForm(UserCreationForm):
    email = forms.EmailField()

    class Meta:
        model = User
        fields = ["username", "email", "password1", "password2"]

Walkthrough: register view

On GET, show an empty form. On POST, bind the form to request.POST, and if form.is_valid(), save, add a success message, and redirect to login (or home). Invalid forms re-render with errors.

Wire /register/ in the users (or project) URLconf and link it from the nav for anonymous visitors.

from django.shortcuts import render, redirect
from django.contrib import messages
from .forms import UserRegisterForm

def register(request):
    if request.method == "POST":
        form = UserRegisterForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get("username")
            messages.success(request, f"Account created for {username}.")
            return redirect("login")
    else:
        form = UserRegisterForm()
    return render(request, "users/register.html", {"form": form})

How to run it and what you should see

Run 05-User-Registration, open /register/, submit valid data, and confirm the success message. Try a duplicate username and mismatched passwords to see validation errors.

git clone https://github.com/michaeldunga1/fcc-django-blog.git
cd fcc-django-blog/05-User-Registration
python3 -m venv .venv
source .venv/bin/activate
pip install -r ../requirements.txt
python manage.py migrate
python manage.py runserver

Common mistakes and troubleshooting

If the form always looks empty after POST, you may be recreating a blank form instead of reusing the bound one when invalid. If messages never show, confirm the messages context processor and a messages loop in base.html.

Crispy errors often mean crispy_forms is missing from INSTALLED_APPS or the template pack setting is unset.

Try this

Register two users and confirm both appear under Users in admin. Intentionally submit an invalid form and read every error message Django returns.

  • Never store plain-text passwords—forms hash for you
  • Redirect after successful POST
  • Show validation errors next to fields

Next: Login and Logout

Comments

One comment per signed-in account. Comments are saved with this page’s URL.