0
0
Djangoframework~5 mins

Registration with UserCreationForm in Django

Choose your learning style9 modes available
Introduction

UserCreationForm helps you quickly create a registration form for new users without writing all the code yourself.

When you want to add a signup page to your website.
When you need to create new user accounts with username and password.
When you want to use Django's built-in user model for registration.
When you want a simple, secure way to handle user passwords.
When you want to avoid writing custom validation for user creation.
Syntax
Django
from django.contrib.auth.forms import UserCreationForm

class MyRegistrationForm(UserCreationForm):
    pass

# In views.py
from django.shortcuts import render, redirect
from .forms import MyRegistrationForm

def register(request):
    if request.method == 'POST':
        form = MyRegistrationForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('login')
    else:
        form = MyRegistrationForm()
    return render(request, 'register.html', {'form': form})

UserCreationForm already includes username, password1, and password2 fields.

Use form.save() to create the new user after validation.

Examples
This uses the default UserCreationForm as is.
Django
from django.contrib.auth.forms import UserCreationForm

# Use directly without changes
form = UserCreationForm()
Create a subclass to customize later if needed.
Django
from django.contrib.auth.forms import UserCreationForm

class CustomUserCreationForm(UserCreationForm):
    pass
Basic view function to handle registration form display and submission.
Django
from django.shortcuts import render, redirect
from django.contrib.auth.forms import UserCreationForm

def register(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('login')
    else:
        form = UserCreationForm()
    return render(request, 'register.html', {'form': form})
Sample Program

This Django view handles user registration. It shows the form on GET requests and processes the form on POST requests. If the form is valid, it saves the new user and redirects to the login page.

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

def register(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('login')
    else:
        form = UserCreationForm()
    return render(request, 'register.html', {'form': form})
OutputSuccess
Important Notes

Always use HTTPS to protect passwords during registration.

UserCreationForm validates that passwords match and meet minimum security requirements.

Customize the form if you want to add extra fields like email.

Summary

UserCreationForm simplifies making user signup forms.

It handles password validation and user creation securely.

Use it in your view to show and process registration forms easily.