Welcome to our comprehensive Django User Registration tutorial! In this lesson, we'll dive deep into creating a user registration system using Django. We'll cover everything from setting up a project, creating a registration form, handling user creation, and even a small quiz to test your understanding. Let's get started!
Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It's built by experienced developers and takes care of much of the hassle of web development, so you can focus on writing your app without needing to reinvent the wheel.
First, make sure you have Python and Django installed. If not, you can find the installation instructions here.
Once installed, create a new Django project:
django-admin startproject mysite
cd mysiteNow, let's create a new app called registration.
python manage.py startapp registrationDjango comes with a built-in User model that handles user authentication. We'll use this model to manage our users.
# registration/models.py
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
class CustomUserManager(BaseUserManager):
pass
class CustomUser(AbstractBaseUser):
email = models.EmailField(_('email address'), unique=True)
is_active = models.BooleanField(_('active'), default=True)
is_staff = models.BooleanField(_('staff status'), default=False)
date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
objects = CustomUserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']Next, we'll create a registration form. This form will be used to gather user information during the registration process.
# registration/forms.py
from django import forms
from django.contrib.auth.forms import UserCreationForm
from .models import CustomUser
class CustomUserCreationForm(UserCreationForm):
email = forms.EmailField(required=True)
class Meta:
model = CustomUser
fields = ('username', 'email', 'password1', 'password2')Now, let's migrate our database to create the new CustomUser table.
python manage.py makemigrations
python manage.py migrateNext, we'll create a view to handle the registration process.
# registration/views.py
from django.shortcuts import render, redirect
from django.contrib.auth import login, authenticate
from django.contrib.auth.forms import LoginForm
from .forms import CustomUserCreationForm
from django.contrib.auth.decorators import login_required
def register(request):
if request.method == 'POST':
form = CustomUserCreationForm(request.POST)
if form.is_valid():
user = form.save()
login(request, user)
return redirect('home')
else:
form = CustomUserCreationForm()
return render(request, 'registration/register.html', {'form': form})
@login_required
def user_login(request):
if request.method == 'POST':
form = LoginForm(data=request.POST)
if form.is_valid():
user = authenticate(request, username=form.cleaned_data.get('username'), password=form.cleaned_data.get('password'))
if user is not None:
login(request, user)
return redirect('home')
else:
form = LoginForm()
return render(request, 'registration/login.html', {'form': form})Finally, we'll create the registration and login templates.
touch registration/templates/registration/register.html
touch registration/templates/registration/login.htmlNow, let's test our registration system.
python manage.py runserverWhat does Django handle automatically in web development?
That's it for our User Registration tutorial! You now have a basic understanding of how to create a user registration system using Django. Keep exploring and practicing to master Django and create amazing web applications! ππ