Django provide user authentication system to manage secure Login, Registration and Logout.
Code of views.py
from django.shortcuts import render,redirect
from django.http import HttpResponse
from . models import Register
from django.contrib.auth.models import User
from django.contrib.auth import authenticate
from django.contrib.auth import logout,login
def index(request):
return HttpResponse('Hello World')
def addition(request):
if request.method=='POST':
a,b = int(request.POST['txtnum1']),int(request.POST['txtnum2'])
c=a+b
return render(request,'helloapp/addition.html',{'key':c})
else:
return render(request,'helloapp/addition.html')
def reg(request):
if request.method=='POST':
#obj = Register(username=request.POST['txtuser'],emailid=request.POST['txtemail'],password=request.POST['txtpass'],mobile=request.POST['txtmobile'],fullname=request.POST['txtfname'])
#obj.save()
user = User.objects.create_user(request.POST['txtuser'],request.POST['txtemail'],request.POST['txtpass'])
user.first_name=request.POST['txtfirst']
user.last_name=request.POST['txtlast']
user.save()
return render(request,"helloapp/reg.html",{"key":"data inserted successfully"})
else:
return render(request,"helloapp/reg.html")
def viewreg(request):
if request.user.is_authenticated:
obj = Register.objects.all()
return render(request,"helloapp/viewreg.html",{"key":obj})
else:
return redirect('/helloapp/loginuser')
def loginuser(request):
if request.method=='POST':
obj = authenticate(username=request.POST['txtemail'],password=request.POST['txtpass'])
if obj is not None:
login(request,obj)
#request.session['uid']=request.POST['txtemail']
return redirect('/helloapp/viewreg')
else:
return render(request,"helloapp/login.html",{"key":"invalid emailid and password"})
else:
return render(request,"helloapp/login.html")
def logoutuser(request):
#del request.session['uid']
logout(request)
return redirect('/helloapp/loginuser')
def setcookie(request):
response = HttpResponse("Cookie Set")
response.set_cookie('ckey', 'hello')
return response
def getcookie(request):
a = request.COOKIES['ckey']
return HttpResponse("value is "+ a)
code of urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('ram', views.addition, name='addition'),
path('reg', views.reg, name='reg'),
path('loginuser', views.loginuser, name='loginuser'),
path('logoutuser', views.logoutuser, name='logoutuser'),
path('getcookie', views.getcookie, name='getcookie'),
path('setcookie', views.setcookie, name='setcookie'),
path('viewreg', views.viewreg, name='viewreg')
]
code of login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Login Form</h1>
<a href="/helloapp/reg">new user register here</a>
<form method="post">
{% csrf_token %}
<p><input type="text" placeholder="Enter username" name="txtemail" /></p>
<p><input type="password" placeholder="Enter password" name="txtpass" /></p>
<p><input type="submit" value="Login" name="btnInsert" /></p>
</form>
{{key}}
</body>
</html>
code of reg.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Registration Form</h1>
<form method="post">
{% csrf_token %}
<p><input type="text" placeholder="Enter username" name="txtuser" /></p>
<p><input type="text" placeholder="Enter password" name="txtpass" /></p>
<p><input type="text" placeholder="Enter email" name="txtemail" /></p>
<p><input type="text" placeholder="Enter firstname" name="txtfirst" /></p>
<p><input type="text" placeholder="Enter lastname" name="txtlast" /></p>
<p><input type="submit" value="Registration" name="btnInsert" /></p>
</form>
<a href="/helloapp/login">user login here</a>
{{key}}
</body>
</html>
code of viewreg.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>All User Information</h1> <a href="/helloapp/logoutuser">Logout</a>
<hr>
<table border="1">
<tr><th>Username</th><th>Password</th><th>Mobileno</th><th>Fullname</th></tr>
{% for data in key %}
<tr><td>{{data.username}} </td><td> {{data.emailid}}</td><td> {{data.mobileno}}</td><td>{{data.fullname}}</td></tr>
{% endfor %}
</table>
</body>
</html>
POST Answer of Questions and ASK to Doubt