User Registration Form
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.
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.
<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.
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. #}
- Saving the raw password typed by the user instead of using UserCreationForm, which hashes it automatically.
- Forgetting to call form.save() inside the POST branch, so the account is never actually created.
- Not logging the user in immediately after registration, forcing them to log in again right after signing up.
- 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.
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