← Back to Django Course | Chapter 10: Authentication & Authorization | Lesson 5 of 12

User Registration Form

A registration form is the sign-up sheet where a new visitor writes their name and picks a secret password to join.

Using UserCreationForm

UserCreationForm handles username, password, and password confirmation fields, plus hashing, without you writing any of that logic.

Example: Using UserCreationForm

form.save() creates the User with a properly hashed password; login() then signs them in right away.

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

def register_view(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, 'register.html', {'form': form})
{# 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. #}

Rendering the Registration Form

The template loops through form fields so each one gets its own label and input automatically.

Example: Rendering the Registration Form

form.as_p renders each field wrapped in a <p>, each with a matching <label> and name attribute already built in.

markup
<form method="post">
  {% csrf_token %}
  {{ form.as_p }}
  <button type="submit">Register</button>
</form>
{# 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. #}

Wiring the Registration URL

The register view needs a URL entry so users can reach the sign-up page.

Example: Wiring the Registration URL

This maps /register/ to register_view, matching the form's action target.

markup
from django.urls import path
from . import views

urlpatterns = [
    path('register/', views.register_view, name='register'),
]
{# 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. #}
Common Mistakes
  1. Saving the raw password typed by the user instead of using UserCreationForm, which hashes it automatically.
  2. Forgetting to call form.save() inside the POST branch, so the account is never actually created.
  3. Not logging the user in immediately after registration, forcing them to log in again right after signing up.
Chapter Summary
  • UserCreationForm is Django's built-in form for registering new users with a hashed password.
  • It validates that the two password fields match before saving.
  • form.save() creates and returns the new User instance.
  • Optionally log the new user in immediately after registration for a smoother experience.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.