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 manage.py 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 manage.py migrate

Now run again the command to create superuser.

It will ask for username(required)

Email address(we can skip)

Password(required)

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 http://127.0.0.1:8000/admin (make sure django server is running (python manage.py 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">

  <head>

    <meta charset="utf-8">

    <title>Signup</title>

  </head>

  <body align="center">

    <br/>

    {% if msg %}

    {{msg}}

    {% endif %}

    <br/>

    <h2> Signup page!</h2>

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

      {% csrf_token %}

      <label>Username</label><br/>

      <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/>

      <label>Password</label><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">

    </form>

  </body>

</html>

Inside accounts/urls.py add path for signup view

from django.urls import path

from . import views

urlpatterns = [

    path('',views.home,name='home'),

    path('signup/',views.signup,name='signup')

 ]

In accounts/views.py define function to process requests landing on http://127.0.0.1:8000/signup/

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":

        username=request.POST['username']

        fname=request.POST['fname']

        lname=request.POST['lname']

        password1=request.POST['password1']

        password2=request.POST['password2']

        if password1 != password2:

            msg="password did not match"

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

        else:

            try:

                user=User.objects.get(username=username)

                msg="username already exists"

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

            except User.DoesNotExist:

                user=User(username=username,password=password1,first_name=fname,last_name=lname)

                user.save()

                login(request,user)

                return redirect('home')

    else:

        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

<html>

<head>

  <title>Home</title>

</head>

<body align="center">

  <br/>

  <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 %}

</body>

</html>

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