How to Create a Registration View in Django: Simple Guide
To create a registration view in Django, define a form class with user fields, then create a view function or class that handles form display and submission using
django.shortcuts.render and django.contrib.auth.models.User. Use POST method to save new users and redirect after successful registration.Syntax
A registration view in Django typically involves these parts:
- Form class: Defines the fields users fill out (e.g., username, password).
- View function or class: Handles GET to show the form and POST to process data.
- Template: HTML form to collect user input.
- Redirect: After successful registration, redirect to login or home page.
python
from django import forms from django.contrib.auth.models import User from django.shortcuts import render, redirect class RegistrationForm(forms.ModelForm): password = forms.CharField(widget=forms.PasswordInput) class Meta: model = User fields = ['username', 'email', 'password'] def register_view(request): if request.method == 'POST': form = RegistrationForm(request.POST) if form.is_valid(): user = form.save(commit=False) user.set_password(form.cleaned_data['password']) user.save() return redirect('login') else: form = RegistrationForm() return render(request, 'register.html', {'form': form})
Example
This example shows a complete registration view using Django's ModelForm for user creation, handling form display and submission, and redirecting after success.
The template register.html should render the form and submit it.
python
from django import forms from django.contrib.auth.models import User from django.shortcuts import render, redirect class RegistrationForm(forms.ModelForm): password = forms.CharField(widget=forms.PasswordInput) class Meta: model = User fields = ['username', 'email', 'password'] def register_view(request): if request.method == 'POST': form = RegistrationForm(request.POST) if form.is_valid(): user = form.save(commit=False) user.set_password(form.cleaned_data['password']) user.save() return redirect('login') else: form = RegistrationForm() return render(request, 'register.html', {'form': form})
Output
When visiting the registration URL, the user sees a form with username, email, and password fields. On submitting valid data, the user is created and redirected to the login page.
Common Pitfalls
Common mistakes when creating a registration view include:
- Not hashing the password before saving, which is insecure.
- Not validating form data, leading to errors or invalid users.
- Forgetting to handle GET and POST methods separately.
- Not redirecting after successful registration, causing form resubmission on refresh.
python
from django import forms from django.contrib.auth.models import User from django.shortcuts import render, redirect # Wrong: saving password as plain text class BadRegistrationForm(forms.ModelForm): class Meta: model = User fields = ['username', 'email', 'password'] def bad_register_view(request): if request.method == 'POST': form = BadRegistrationForm(request.POST) if form.is_valid(): form.save() # Password not hashed! return redirect('login') else: form = BadRegistrationForm() return render(request, 'register.html', {'form': form}) # Right: use set_password to hash class GoodRegistrationForm(forms.ModelForm): password = forms.CharField(widget=forms.PasswordInput) class Meta: model = User fields = ['username', 'email', 'password'] def good_register_view(request): if request.method == 'POST': form = GoodRegistrationForm(request.POST) if form.is_valid(): user = form.save(commit=False) user.set_password(form.cleaned_data['password']) user.save() return redirect('login') else: form = GoodRegistrationForm() return render(request, 'register.html', {'form': form})
Quick Reference
- Use
ModelFormto simplify user form creation. - Always hash passwords with
set_password()before saving. - Separate GET (show form) and POST (process form) in your view.
- Redirect after successful registration to avoid duplicate submissions.
- Use Django's built-in
Usermodel or customize if needed.
Key Takeaways
Create a form class to collect user registration data.
Hash passwords using set_password before saving users.
Handle GET and POST requests separately in the view.
Redirect after successful registration to prevent resubmission.
Use Django's User model and ModelForm for simplicity.