How to set up Django built-in Registration in 2023

Don EsromDon Esrom
4 min read

Setting up user registration allows your potential users can create accounts and store personal info on your web app. Currently, Django offers a number of different ways to set this up.

One tried and tested method is the Django built-in authentication system that provides you with a ready-made process that doesn't require you to use any third-party processes. This article will explore the Django built-in authentication system and teach you how to set it up.

What is Django Built-in Registration System

The Django built-in registration system is a simple and secure way for users to create accounts and log into your website.

Using this built-in system, businesses and or organizations that require users to maintain accounts such as ecommerce stores, school accounts, and social media sites can collect user information securely.

Some of the features offered by Django's built-in registration include user registration, email verification, password management, and user profiles.

How to set up User Registration using Django Built-in Authentication

Below are the steps required to set up User registration for your new users.

Step 1: Set up auth views and URLs on the main urls.py file.

Go to your main urls.py file and add the following code.

from django.contrib.auth import views as auth_views
from django.urls import path, include

urlpatterns = [

    ..

    path('accounts/', include('django.contrib.auth.urls')),

    ...

]

Step 2: Create a User registration form

The second step to creating your Django built-in Registration includes creating your Registration Form. The registration form provides all the necessary fields required for potential users to input their information before they can create an account within your web application.

This will also act as the base for our entire process. To create a registration form, go to your application and create a forms.py file. Within the file, type:

From Django import forms 
From Django.contrib.auth.forms import UserCreationForm
From Django.contrib.auth.models import User

Class RegistrationForm(UserCreationForm):
    Email = forms.EmailField(required-True)

    Class Meta:
        Model = User
        Fields = [‘username’, ‘email’, ‘password1’, ‘password2’]

Step 3: Create Registration Views

Once done it is now time to work on our user creation views. Assuming you are familiar with the Django web framework, you understand that the views control what is rendered on the template. It also handles the logic part of the website.

from django.contrib.auth.forms import *

def sign_up(request):
    if request.method == ‘POST’:
        form = RegistrationForm(request.post)
        if form.is_valid():
        user = form.save()
        login(request,user)
        return redirect(‘/)
    else:
        form = RegistrationForm()
return render(request, ‘register.html’, {‘form’:form})

Step 4: Include Registration URLs

Once you have your views in place, you need to set up your registration URLs. These will allow users to visit the Registration page and register. To do this, go to your local urls.py file and add this:

Path(‘sign_up’ views.sign_up, name = ‘sign_up’)

Step 5: Create a registration Template

By now, your registration page is almost ready. All you need to do is configure the HTML template to provide the form fields that you created on the forms.py file that will allow users to input their information and register. To do this, go to your register.html file and type this:

<h2>Sign up</h2>
    <form method="post">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit">Sign up</button>
    </form>

{% endblock %}

You can make the form more beautiful and dynamic by using crispy forms. This is a third-party solution by Bootstrap designed to help you make different forms more beautiful. Click here to read more about Crispy Forms or watch this 3-minute video to learn how to install and use crispy forms on your Django projects.

NOTE: The CSRF token that’s added to the template is one of the most important features provided by Django to keep your information safe. You will encounter an error if you try to run your web form without a CSRF token. Learn more about the CSRF token here.

Conclusion

Congratulations, your registration process is working thanks to Django’s built-in registration process. Our next blog post will explore user authentication using the Django built-in authentication process to allow you to build even bigger projects using Django.

0
Subscribe to my newsletter

Read articles from Don Esrom directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Don Esrom
Don Esrom

Don is a freelance tech content writer and software developer. When he is not writing content or code, he is building or dismantling things with his hands... or taking a nap.