Django Tutorial: User Registration 🎯

beginner
25 min

Django Tutorial: User Registration 🎯

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!

What is Django? πŸ“

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.

Setting Up the Project πŸ“

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:

bash
django-admin startproject mysite cd mysite

Creating the Registration App πŸ“

Now, let's create a new app called registration.

bash
python manage.py startapp registration

User Models πŸ“

Django comes with a built-in User model that handles user authentication. We'll use this model to manage our users.

python
# 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']

Creating the Registration Form πŸ“

Next, we'll create a registration form. This form will be used to gather user information during the registration process.

python
# 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')

Migrating the Database βœ…

Now, let's migrate our database to create the new CustomUser table.

bash
python manage.py makemigrations python manage.py migrate

Creating the Registration View πŸ“

Next, we'll create a view to handle the registration process.

python
# 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})

Creating Registration Templates πŸ“

Finally, we'll create the registration and login templates.

bash
touch registration/templates/registration/register.html touch registration/templates/registration/login.html

Testing the Registration System βœ…

Now, let's test our registration system.

  1. Run the development server: python manage.py runserver
  2. Open a web browser and navigate to http://127.0.0.1:8000/accounts/register/
  3. Fill out the registration form and click "Register"
  4. Open the login page (http://127.0.0.1:8000/accounts/login/) and use the registered email and password to log in

Quiz 🎯

Quick Quiz
Question 1 of 1

What 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! πŸš€πŸŒŸ