How can you
create custom login and logout using
session of django framework ?
First of install python and django to your system.
Then follow
Given steps:-
Create mypl project folder and inside it create myapp
folder as shown following structure:-
D:\>env\Scripts\activate
run the following command
- This will create a mypl directory
in your D: drive
D:\>django-admin
startproject mypl
Then go to mypl folder path
type following command :-
D:\>cd mypl
To create your app, make sure
you’re in the your project directory & type this command:
D:\mypl\>python
manage.py startapp myapp
your folder structure will be like this
now open mypl fodler in visual code and start your coding step b step .
1)Models.py file code :-
class Myrecords(models.Model):
fullname= models.CharField(max_length=100)
email= models.EmailField()
contact= models.CharField(max_length=50)
pd= models.CharField(max_length=200)
2)views.py code :-
from django.shortcuts import render,redirect
from .models import Myrecords
# Create your views here.
def about(request):
return render(request, 'about.html')
def index(request):
return render(request, 'index.html')
def add(request):
return render(request,'add.html')
def login(request):
return render(request,'login.html')
def dashboard(request):
return render(request,'dashboard.html')
def addrecord(request):
fullname=request.POST['fullname']
email=request.POST['email']
contact=request.POST.get('contact', False)
pd=request.POST['pd']
member = Myrecords(fullname=fullname, email=email,contact=contact,pd=pd)
member.save()
data="inserted"
return render(request,'add.html',{'data':data})
def trainerlogin(request):
msg=""
if request.method=='POST':
x=request.POST['em']
y=request.POST['pwd']
trainer=Myrecords.objects.filter(em=x,pd=y).count()
if trainer> 0 :
request.session['trainerLogin']=True
return redirect('dashboard/')
else:
msg="Invalid!"
return render(request,'login.html',{'msg':msg})
def logout(request):
del request.session['trainerLogin']
return redirect('login/')
3)urls.py code of app folder :-
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('about/', views.about, name='about'),
path('add/', views.add, name='add'),
path('login/', views.login, name='login'),
path('logout/', views.logout, name='logout'),
path('add/addrecord/', views.addrecord, name='addrecord'),
path('login/trainerlogin/', views.trainerlogin, name='trainerlogin'),
path('login/trainerlogin/dashboard/',views.dashboard,name='dashboard')
]
4)urls.py code of project folder :-
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('myapp/', include('myapp.urls')),
]
5)create templates folder inside your app folder and write code for html files.
dashboard.html file code.
<ul class="navbar-nav mr-auto">
{% if request.session.trainerLogin %}
<li class="nav-item">
<a class="nav-link" href="{% url 'logout' %}">Logout</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Welcome,login sucessful</a>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link" href="{% url 'login' %}">Login</a>
</li>
{% endif %}
</ul>
login.html file code :-
<h1>login form</h1>
<form action="trainerlogin/" method="post">
{% csrf_token %}
email:<br>
<input name="em">
<br><br>
Password:<br>
<input name="pd">
<br><br>
<input type="submit" value="login">
</form>
{{msg}}
add.html file code :-
<h1>Add member</h1>
<form action="addrecord/" method="post">
{% csrf_token %}
First Name:<br>
<input name="fullname">
<br><br>
email:<br>
<input name="email">
<br><br>
cotnact:<br>
<input name="cotnact">
<br><br>
Password:<br>
<input name="pd">
<br><br>
<input type="submit" value="Submit">
</form>
{{data}}
index.html :-
<h1> this is hom page</h1>
about.html :-
<h1> this is about page</h1>
6)
Sign up here with your email
ConversionConversion EmoticonEmoticon