Creation of webpages for register ,login ,logout (PART-1)

Posted 3 years, 4 months ago
Views 551
4 min read
3 reactions

Hi techions , welcome to my blog

Today let us know how to build a webpage to register using django and python. 

So for any webpage or website in django first we have to create an project and create app by activating the virtual environment.

Step 1:

 Creating a project with a name

$ django-admin startproject reg

Step 2:

Creating an app with a name

$ python startapp accounts

Step 3:

Creating file in accounts app.The file in django is related to database 

i.e., it stores information in database at the server side and also helps in retrieving information required.

So, the information we needed about the user is id (Django adds an id field to each 

model, which is used as the primary key for that model), name,email,password .So these are given as fields in models file to create data base. 

As you can see below the models fields are declared in this way….

from django.db import models

class User(models.Model):

 Step 4:

 Now let's move to the file..

from django.shortcuts import render,redirect
from django.contrib.auth.models  import User,auth
from django.contrib import messages

def register(request):
    if request.method=='POST': (#post method refers to the information taken from user)
        if password1==password2:
            if User.objects.filter(username=username).exists(): (# checks if a person with same username already exists)
      ,'username taken') (# if exists it displays this message) 
                return redirect('register')
            elif User.objects.filter(email=email).exists(): (# checks if a person with same email already exists)
      ,'emailtaken') (# if exists it displays this message) 
                return redirect('register')
       (#if both the conditions fail then it saves the user information into the database)
      ,'registration successful')
                return redirect('login')
  ,'password not matching')
            return redirect('register')    
        return render(request,'start/register.html') 

If we observe the above code we can see that it represents the functionality I.e.,what action it should perform if we submit or what information it should save in the database. 

Here the POST method refers that the information should be taken from the client (user) and it 

should be saved in the server .

Step 5: 

Let's give it a path in urls. py file of accounts app and also create urls. py file in regi folder.

accounts/ file

from django.contrib import admin
from django.urls import path,include
from . import views

urlpatterns = [
regi/ file

from django.contrib import admin
from django.urls import path,include

urlpatterns = [

Step 6:

Register the user in accounts/ file

from django.contrib import admin
from . models import User

 Step 7:

          Creating template register.html file

(the path should be templates/start/register.html)

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    {% load static %}
    <link rel="stylesheet" type="text/css" href="{% static 'css/style2.css' %}">
    <link rel="stylesheet" href="" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
    <title>REGISTER PAGE</title>
      <div class="main">
    <div class="left" style="float: right;">
        <h4><b>Register Here</b></h4>
           <img src="" height="370px" >
        <div class="right" >
           <form   method="POST">
              {% csrf_token %}
                <div class="form-group">
                  <label>User Name</label>
                  <input type="text" class="form-control " name="username" >
               <div class="form-group">
                   <input type="email" class="form-control " name="email" >
              <div class="form-group">
                  <input type="password" class="form-control " name="password1" minlength="8">
              <div class="form-group">
                  <label> Confirm Password</label>
                  <input type="password" class="form-control " name="password2" minlength="8">
                  <button type="submit" class="btn btn-secondary" style="width: 100%; margin-left:auto">Submit</button>
           <div style="text-align:center;">
              {% for message in messages %}
              <h4> {{message}}</h4>
              {% endfor %}

Step8 :

 Apply migrations and run output

$ python makemigrations

$ python migrate

And see the output by 

 python runserver  

and in the path accounts/register.

So ,it is the ending of part-1 register page

In part-2 let's learn how to create a login and logout pages using django and also how to create static files and apply css...

3 reactions


Joined on Aug. 27, 2020

Latest Videos