Creating signup system in Django

In this tutorial we will first create a super user to login to Django admin page. Then we will add a url and view for our registration page. After this we will create a registration template and at last, we will add a link in our home page to navigate the user to registration page.

Create superuser.

python createsuperuser

if you get error like below.

django.db.utils.OperationalError: no such table: auth_user

This means that the auth_user table required to execute the command is not available. This is inbuilt table in Django which we can use just by migrating the available changes by using the below command.

python migrate

Now run again the command to create superuser.

It will ask for username(required)

Email address(we can skip)


Password(again) (required)

Once done with above fields, you will get message as below

Superuser created successfully.

You can use this username and password to access the Django admin page.

Visit (make sure django server is running (python runserver))

Type in username and password and you should be able to access admin page.


Now click logout to get out of the admin panel.

Now we will create a signup template.

Create signup.html file in templates/accounts (in same path where we created home.html in our previous tutorial) and add the following html code for signup page

<!DOCTYPE html>

<html lang="en" dir="ltr">


    <meta charset="utf-8">



  <body align="center">


    {% if msg %}


    {% endif %}


    <h2> Signup page!</h2>

    <form action="" method="post">

      {% csrf_token %}


      <input type="text" name="username" required><br/><br/>

      <label>First name</label><br/>

      <input type="text" name="fname" required><br/><br/>

      <label>Last name</label><br/>

      <input type="text" name="lname" required><br/><br/>


      <input type="password" name="password1" required><br/><br/>

      <label>Confirm password</label><br/>

      <input type="password" name="password2" required><br/><br/>

      <input type="submit" value="Register">




Inside accounts/ add path for signup view

from django.urls import path

from . import views

urlpatterns = [




In accounts/ define function to process requests landing on

from django.shortcuts import render,redirect

from django.contrib.auth import authenticate,login

from django.contrib.auth.models import User

def home(request):

    return render(request,'accounts/home.html')

def signup(request):

    if request.method=="POST":






        if password1 != password2:

            msg="password did not match"

            return render(request,'accounts/signup.html',{'msg':msg})




                msg="username already exists"

                return render(request,'accounts/signup.html',{'msg':msg})

            except User.DoesNotExist:




                return redirect('home')


        return render(request,'accounts/signup.html')

Now we will add a link in our homepage for user to register. So in home.html file add below html code





<body align="center">


  <h2>This is our home page</h2><br/><br/><br/>

  {% if user.is_authenticated %}

  <h3> Hello {{user.first_name}}</h3>

  {% else %}

  <a href="{% url "signup" %}">Register</a>

  {% endif %}



After filling up the signup form correctly click on Register button, we will land up in our home page. Our Registeration page will look similar to below image.

Published : July 18, 2020