Django start project | Login and Registration form (Django)

Django start project- In the starting of django we will learn the steps of making Registration and login page,we will start django new project, here we will not use django admin. we will use django programming , you can easily make django ecommerce website, so welcome in django learning

Registration and login project

 Step 1: Folder name  :  loginandregistration
 Step 2: Project name:   login
 Step 3:app name:          regandloginapp
 Step 4:now open pycharm
go to models.py file and write
_______________________________________________________________________
from __future__ import unicode_literals
from django.db import models

class Reg(models.Model):
    fname=models.CharField(max_length=20)
    lname=models.CharField(max_length=20)
    user=models.CharField(primary_key=True,max_length=20)
    pwd=models.CharField(max_length=20)
    mobile=models.CharField(max_length=20,unique=True)
    email=models.EmailField(max_length=20,unique=True)
    dob=models.DateField(null=True)
    gendar=models.CharField(max_length=20,blank=True,null=True)
_______________________________________________________________________
Step 5:
make forms.py file in app level file Right click and new select python file
goto forms.py file and write 
________________________________________________________________________
from django import forms
from .models import Reg
from django import forms
class RegForm(forms.Form):
    fname=forms.CharField(
        widget=forms.TextInput(
            attrs={
                ‘class’:’form-control’,
                ‘placeholder’:’Enter First name’
            }
        )
    )
    lname = forms.CharField(
        widget=forms.TextInput(
            attrs={
                ‘class’: ‘form-control’,
                ‘placeholder’: ‘Enter Last name’
            }
        )
    )
    user = forms.CharField(
        widget=forms.TextInput(
            attrs={
                ‘class’: ‘form-control’,
                ‘placeholder’: ‘Enter user name’
            }
        )
    )
    pwd = forms.CharField(
        widget=forms.PasswordInput(
            attrs={
                ‘class’: ‘form-control’,
                ‘placeholder’: ‘Enter Password’
            }
        )
    )
    mobile = forms.CharField(
        widget=forms.NumberInput(
            attrs={
                ‘class’: ‘form-control’,
                ‘placeholder’: ‘Enter Mobile name’
            }
        )
    )
    email = forms.CharField(
        widget=forms.EmailInput(
            attrs={
                ‘class’: ‘form-control’,
                ‘placeholder’: ‘Enter Email’
            }
        )
    )
    dob = forms.DateField(
        widget=forms.DateInput(
            attrs={
                ‘class’: ‘form-control’,
                ‘placeholder’: ‘Enter Date of birth’
            }
        )
    )
    gender = forms.CharField(
        widget=forms.TextInput(
            attrs={
                ‘class’: ‘form-control’,
                ‘placeholder’: ‘Enter Gender’
            }
        )
    )

class LoginForm(forms.Form):
    user=forms.CharField(max_length=20)
    pwd=forms.CharField(widget=forms.PasswordInput())

____________________________________________________________________________
Step 6:Go to project level url.py file
____________________________________________________________________________

from django.contrib import admin
from django.conf.urls import url
from django.conf.urls import include

urlpatterns = [
    url(‘admin/’, admin.site.urls),
    url(‘regandloginapp/’,include(‘regandloginapp.urls’)),
]
______________________________________________________________________________
Step 7:create url.py file in regandloginapp app
Right click—>new—>python file–>url.py
goto the file and write the code
______________________________________________________________________________
from django.conf.urls import url
from .import views
app_name=’regandloginapp’
urlpatterns=[
    url(‘^$’,views.home,name=’home’),
    url(‘^reg$’,views.reg,name=’reg’),
    url(‘^login$’,views.login,name=’login’),
]
________________________________________________________________________________
Step 8:
Goto views.py file
and write
_________________________________________________________________________________
from django.shortcuts import render
from django.http import HttpResponseRedirect,HttpResponse
from .models import Reg
from .forms import LoginForm,RegForm
def home(request):
    return render(request,’home.html’)
def reg(request):
    if request.method==”POST”:
        form=RegForm(request.POST)
        if form.is_valid():
            fname=request.POST.get(‘fname’,”)
            lname=request.POST.get(‘lname’,”)
            user=request.POST.get(‘user’,”)
            pwd=request.POST.get(‘pwd’,”)
            mobile=request.POST.get(‘mobile’,”)
            email=request.POST.get(’email’,”)
            dob=request.POST.get(‘dob’,”)
            gendar=request.POST.get(‘gendar’,”)
        reg=Reg(fname=fname,lname=lname,user=user,pwd=pwd,mobile=mobile,email=email,dob=dob,gendar=gendar)
        reg.save()
        return HttpResponse(‘reg success’)
    else:
        form=RegForm()
        return render(request,’reg.html’,{‘form’:form})
 
def login(request):
    if request.method==”POST”:
        MyLoginForm=LoginForm(request.POST)
        if MyLoginForm.is_valid():
            user=MyLoginForm.cleaned_data[‘user’]
            pwd=MyLoginForm.cleaned_data[‘pwd’]
            dbuser=Reg.objects.filter(user=user)
            dbpwd=Reg.objects.filter(pwd=pwd)
            if not dbuser or dbpwd:
                return HttpResponse(‘login success’)
            else:
                return HttpResponse(‘login failed’)
    else:
        form=LoginForm()
        return render(request,’login.html’,{‘form’:form})
         
__________________________________________________________________________
Step 9:create templates folder in the project folder wherever manage.py  and there make home.html file
and write the below code
___________________________________________________________________________
<html>
<head>
    <style>
        div{
            padding-top: 20px;

        }
        i{
            color:red;
        }
        .h1{
            color:green;
        }
        #id1
        {
            padding-top: 130px;
            font-size: x-large;
            font-style: italic;
        }
    </style>

</head>
<body bgcolor=”#ooffff”>
<div align=”center”>
           <h1 class=”h1″>welcome to <i> Rajat</i>world</h1>
            <h2 ><a href=”./reg”>click here to register</a></h2>
            <h2 ><a href=”./login”>click here to login</h2>
</div>
<pre id=”id1″ style=”float:right;”>
    <b>By</b>
    Rajat upadhyaya
    9559513070
</pre>

</body>
</html>
__________________________________________________________________________
Step 9:create html file with name reg.html in templates folder
_________________________________________________________________________
<!DOCTYPE html>
<html lang=”en”>
<head>
   <meta charset=”UTF-8″>
    <title>Welcome to Rajat world</title> <meta charset=”utf-8″>
  <meta name=”viewport” content=”width=device-width, initial-scale=1″>
  <link rel=”stylesheet” href=”https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css”>
  <script src=”https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js”></script>
  <script src=”https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js”></script>
  <script src=”https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js”></script>
</head>
<style>
    body{
        background-color: aqua;
    }
    b{
        font-size: 25px;
    }
    form{
        background-color: paleturquoise;
        border:5px solid red;
        border-radius: 5px;
        padding:10px 10px;
    }
    #h1tag{
        color:red;
    }
</style>
<body>
<h1 class=”text-center” id=”h1tag”>welcome to Rajat world</h1>
<div class=”container”>
    <h4>Please register your details here</h4>
    <div class=”row”>
        <div class=”col-md-4″>
            <form method=”POST”>
                {% csrf_token %}
                {{form}}<br>
                <div class=”text-center”>
                    <input type=”submit”
                           value=”Submit”
                           class=”btn btn-default btn-md”>
                    <input type=”reset”
                           value=”Reset”
                           class=”btn btn-default btn-md”>
             
                </div>
            </form>
        </div>
        <div class=”col-sm-8″>
            <h4 align=”center”><a href=”./”>Goto home page</a> </h4>
            <h4 align=”center”><a href=”./login”>Goto login page</a> </h4>
         
        </div>
    </div>
</div>
</body>
</html>
_________________________________________________________________________________
Step 10:Create Html file with name login.html in templates folder
open file and write the below code
________________________________________________________________________________
<html>
<head>
    <style>
        table{
            margin-top: 30px;
        }
    </style>
</head>
<body bgcolor=”aqua”>
<h3 align=”center”>Please enter your deatials to login</h3>
<form action=”” method=”POST”>
    {%csrf_token%}
    <table bgcolor=”#deb887″ align=”center”>
        <tr> <td>user name: </td><td>{{form.user}}</td></tr>
        <tr> <td>Password: </td><td>{{form.pwd}}</td></tr>
        <tr><td></td><td><input type=”submit” value=”login”></td></tr>
     
    </table><br>
    <h2 align=”center”><a href=”./”>Goto Homepage</a> </h2>
    <h2 align=”center”><a href=”./reg”>Goto Registration Page</a> </h2>
</form>
</body>
</html>
________________________________________________________________________
Step 11:
create database
goto mysql and create database with name dbadmin
mysql>create database dbadmin;
mysql>use dbadmin;

Step 12:
goto setting file and configure the database

DATABASES = {
    ‘default’: {
        ‘ENGINE’: ‘django.db.backends.mysql’,
        ‘NAME’: ‘dbadmin’,
        ‘USER’:’root’,
        ‘PASSWORD’:’root’,
    }
}

Step 13 :
configure templates in settings file
________

TEMPLATES = [
    {
        ‘BACKEND’: ‘django.template.backends.django.DjangoTemplates’,
        ‘DIRS’: [os.path.join(BASE_DIR,’templates’)],
        ‘APP_DIRS’: True,
        ‘OPTIONS’: {
            ‘context_processors’: [
                ‘django.template.context_processors.debug’,
                ‘django.template.context_processors.request’,
                ‘django.contrib.auth.context_processors.auth’,
                ‘django.contrib.messages.context_processors.messages’,
            ],
        },
    },
]
______________
Step 14:
now run makemigrations command
python manage.py makemigrations
and runserver (python manage.py runserver)


now run your project

1 thought on “Django start project | Login and Registration form (Django)”

Leave a Reply

Your email address will not be published. Required fields are marked *