Django login, registration, password reset

Empire my love 2022-05-14 14:53:59 阅读数:420

djangologinregistrationpasswordreset

First create Django project :

To configure settings.py and MySQL I won't say , Own online configuration

 

xiangmu.js file

my MySQL:

link MySQL, I won't talk about mapping database

I am the MySQL The table is mapped to 【inspectdb  +    Table name 】

 

Copied to the models.py In the document , And then map

 

my models.py file :

from django.db import models
# Create your models here.
class Users1(models.Model):
"""
User table : nickname , mailbox , password , Personal profile , Head portrait address
"""
# nick_name = models.CharField(max_length=20)
# email = models.CharField(max_length=50)
# password = models.CharField(max_length=100)
# introduce = models.CharField(max_length=255,blank=True, null=True)
# head = models.ImageField(upload_to='', default='http://img3.duitang.com/uploads/item/201505/22/20150522205616_KeX3C.jpeg')
class Users(models.Model):
nick_name = models.CharField(max_length=20)
email = models.CharField(max_length=50)
password = models.CharField(max_length=100)
class Meta:
managed = False
db_table = 'users'

my views.py file :

# Create your views here.
from django.core.mail import send_mail
from django.shortcuts import render
import random
from test1.models import Users
from django.http import HttpResponse
from django.db.models import Q
# Registration page
def zhuce01(request):
return render(request,'zhuce.html')
# Registration function
def zhuce02(request):
# Get data from browser
uesr1 = request.POST.get('yhm')# user name
yx1 = request.POST.get('yx')# mailbox
pw1 = request.POST.get('mm')# password
wyzm = request.POST.get('yzm')# Verification Code
# Get cached data from browser
yzm = request.session['message']
# Determine if the user name is duplicate
if(Users.objects.filter(nick_name=uesr1)):
del request.session['message']# Delete browser cache
request.session['message'] = ''# Set browser cache message It's empty
return HttpResponse(" repeat of user name , Registration failed !")
# Determine whether the mailbox is duplicate
elif(Users.objects.filter(email=yx1)):
del request.session['message']# Delete browser cache
request.session['message'] = ''# Set browser cache message It's empty
return HttpResponse(" Duplicate mailbox , Registration failed !")
# Determine whether the verification code is the same as the email verification code
elif (wyzm!=yzm):
del request.session['message']# Delete browser cache
request.session['message'] = ''# Set browser cache message It's empty
return HttpResponse(" The verification code is wrong or expired ")
else:
# Determine whether the verification code is the same
if(wyzm==yzm):
del request.session['message'] # Delete browser cache
request.session['message'] = '' # Set browser cache message It's empty
# Store in database
u = Users(nick_name=uesr1,email=yx1,password=pw1)
u.save()
return HttpResponse(" Registered successfully !")
else:
return HttpResponse(" Registration failed !")
# Login page
def dengru01(request):
return render(request,'dengru.html')
# Login function
def dengru02(request):
# Get data from browser
duesr1 = request.POST.get('dyhm')# user name
dpw1 = request.POST.get('dmm')# password
# Exception capture
try:
# Determine whether the user name or mailbox and password match the database
Users.objects.get(Q(Q(nick_name=duesr1)|Q(email=duesr1))&Q(password=dpw1))
except:
return HttpResponse(" Wrong username or password !")
return HttpResponse(' success !')
# Password reset page
def mimachongzhi01(request):
return render(request,'chongzhimima.html')
# Reset password function
def mimachongzhi02(request):
# Get data from the browser
uesr1 = request.POST.get('zyhm')# user name
pw1 = request.POST.get('zxmm1')# password 1
pw2 = request.POST.get('zxmm2')# password 2
zyx = request.POST.get('zyx')# Verification Code
# Get cached data from browser
zyx1 = request.session['message']
# Determine the password , Check whether the verification code is consistent
if((pw1 == pw2)&(zyx == zyx1)):
# Determine whether the mailbox is consistent
u = Users.objects.get(email=uesr1)
# Change password
u.password = pw1
# Put it in the database
u.save()
del request.session['message']# Delete browser cache
request.session['message'] = ''# Set browser cache message It's empty
return HttpResponse(" Reset successful !")
# Judge whether the password and verification code are consistent
elif((pw1 == pw2)&(zyx != zyx1)):
del request.session['message']# Delete browser cache
request.session['message'] = ''# Set browser cache message It's empty
return HttpResponse(" The verification code is wrong or expired !")
# Judge whether the password and verification code are consistent
elif ((pw1 != pw2) & (zyx == zyx1)):
del request.session['message']# Delete browser cache
request.session['message'] = ''# Set browser cache message It's empty
return HttpResponse(" Password inconsistency !")
else:
del request.session['message']# Delete browser cache
request.session['message'] = ''# Set browser cache message It's empty
return HttpResponse(" The passwords are inconsistent or the verification code is wrong !")
def dianjiyianzhengma(request):
# random number
number = str(random.randint(100000, 999999))
# take message Put it in the browser
request.session['message'] = number
# Email verification code
# send_mail(" Reset password ", " Verification code for resetting password :"+number, "[email protected]", ["[email protected]"], fail_silently=True)
return HttpResponse(number)

my urls.py file :

No xiangmu Inside urls.py file , It is untitled2 Medium urls.py file

"""untitled2 URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from xiangmu.views import zhuce01,zhuce02,dengru01,dengru02,mimachongzhi01,mimachongzhi02,dianjiyianzhengma
from System import urls
urlpatterns = [
path('', dengru01),
path('dengru02/', dengru02),
path('zhuce01/', zhuce01),
path('zhuce02/', zhuce02),
path('mimachongzhi01/', mimachongzhi01),
path('mimachongzhi02/', mimachongzhi02),
path('dianjiyianzhengma/', dianjiyianzhengma),
]

my HTML file :

my HTML The document references bootstrap,angular Website , as follows :

<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdn.staticfile.org/angular.js/1.4.6/angular.min.js"></script>

1. Registration page

(1).script The function or condition of judging , You can write it yourself

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title> Registration system </title>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdn.staticfile.org/angular.js/1.4.6/angular.min.js"></script>
<script src="/static/js/xiangmu.js"></script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
<div style="width: 25%;height: 100%;padding: 25px;margin:auto;border: 5px solid black;margin-top: 130px">
<div style="text-align: center">
<h2><a href="/"> Log in </a>----<a href="#"> register </a></h2>
</div>
<div>
<form class="form-horizontal" role="form" method="get" name="myForm">
<div class="form-group">
<p class="col-sm-1"></p>
<p class="col-sm-1"><span class="glyphicon glyphicon-user" style="color: rgb(0, 0, 0); font-size: 20px;top: 7px"></span></p>
<div class="col-sm-8">
<input ng-model="username" name="username" type="text" class="form-control" id="firstname" placeholder=" user name " required>
</div>
<div class="col-sm-2" style="top: 9px">
<span ng-show="myForm.username.$invalid"><span class="glyphicon glyphicon-remove" style="color: rgb(255, 1, 0);"></span></span>
<span ng-show="myForm.username.$valid"><span class="glyphicon glyphicon-ok" style="color: rgb(0, 0, 255);"></span></span>
</div>
</div>
<div class="form-group">
<p class="col-sm-1"></p>
<p class="col-sm-1"><span class="glyphicon glyphicon-envelope" style="color: rgb(0, 0, 0); font-size: 20px;top: 7px"></span></p>
<div class="col-sm-8">
<input ng-model="Email" name="Email" type="email" class="form-control" id="firstname" placeholder=" mailbox " required>
</div>
<div class="col-sm-2" style="top: 9px">
<span ng-show="myForm.Email.$invalid"><span class="glyphicon glyphicon-remove" style="color: rgb(255, 1, 0);"></span></span>
<span ng-show="myForm.Email.$valid"><span class="glyphicon glyphicon-ok" style="color: rgb(0, 0, 255);"></span></span>
</div>
</div>
<div class="form-group">
<p class="col-sm-1"></p>
<p class="col-sm-1"><span class="glyphicon glyphicon-lock" style="color: rgb(0, 0, 0); font-size: 20px;top: 7px"></span></p>
<div class="col-sm-8">
<input ng-model="password" onblur="tname()" name="password" type="text" class="form-control" id="passwordlastname" placeholder=" password " required>
</div>
<div class="col-sm-2" style="top: 9px">
<span ng-show="myForm.password.$invalid"><span class="glyphicon glyphicon-remove" style="color: rgb(255, 1, 0);"></span></span>
<span ng-show="myForm.password.$valid"><span class="glyphicon glyphicon-ok" style="color: rgb(0, 0, 255);"></span></span>
</div>
</div>
<div class="form-group">
<p class="col-sm-1"></p>
<p class="col-sm-1"><span class="glyphicon glyphicon-inbox" style="color: rgb(0, 0, 0); font-size: 20px;top: 7px;"></span></p>
<div class="col-sm-5">
<input ng-model="yianzhengma" type="text" class="form-control" id="lastname" placeholder=" Please enter the verification code ">
</div>
<div class="col-sm-4">
<input ng-click="dianjiyzm()" id="btn" type="button" style="width: 100%" class="btn-block form-control btn-primary obtain generate_code" value=" Get verification code " onclick="settime(this)">
</div>
<p class="col-sm-1"></p>
</div>
<div class="form-group">
<p class="col-sm-1"></p>
<div class="col-sm-10">
<input ng-click="zhuce01()" width="100%" class="btn-block btn btn-primary form-control" type="button" value=" register ">
</div>
<p class="col-sm-1"></p>
</div>
</form>
</div>
</div>
<script type="text/javascript">
// count down
var countdown=3;
function settime(val) {
if (countdown == 0) {
val.removeAttribute("disabled");
val.value=" Get verification code ";
countdown = 3;
return false;
} else {
val.setAttribute("disabled", true);
val.value=" To resend (" + countdown + ")";
countdown--;
}
setTimeout(function() {
settime(val);
},1000);
}
function tname(){
var name=document.getElementById("passwordlastname").value;
if(name.length<6)
alert(" Password must be greater than 6 Characters ")
}
</script>
</body>
</html>

2. Login page

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Login system </title>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdn.staticfile.org/angular.js/1.4.6/angular.min.js"></script>
<script src="/static/js/xiangmu.js"></script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
<div style="width: 25%;height: 100%;padding: 25px;margin:auto;border: 5px solid black;margin-top: 130px">
<div style="text-align: center">
<h2><a href="/"> Log in </a>----<a href="zhuce01"> register </a></h2>
</div>
<div>
<form class="form-horizontal" role="form">
<div class="form-group">
<p class="col-sm-1"></p>
<p class="col-sm-1"><span class="glyphicon glyphicon-user" style="color: rgb(0, 0, 0); font-size: 20px;top: 7px"></span></p>
<div class="col-sm-9">
<input ng-model="usernameemail" type="text" class="form-control" id="firstname" placeholder=" User name or email ">
</div>
<p class="col-sm-1"></p>
</div>
<div class="form-group">
<p class="col-sm-1"></p>
<p class="col-sm-1"><span class="glyphicon glyphicon-lock" style="color: rgb(0, 0, 0); font-size: 20px;top: 7px"></span></p>
<div class="col-sm-9">
<input ng-model="dengrupassword" type="password" class="form-control" id="lastname" placeholder=" password ">
</div>
<p class="col-sm-1"></p>
</div>
<div class="form-group">
<p class="col-sm-1"></p>
<div class="col-sm-10">
<input ng-click="dengru01()" width="100%" class="btn-block btn btn-primary form-control" type="button" value=" Log in ">
</div>
<p class="col-sm-1"></p>
</div>
<div class="form-group">
<h5 style="margin-left: 60%"><a href="mimachongzhi01"> Forget the password , Reset password </a></h5></p>
</div>
</form>
</div>
</div>
</body>
</html>

3. Password reset page

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Reset password </title>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdn.staticfile.org/angular.js/1.4.6/angular.min.js"></script>
<script src="/static/js/xiangmu.js"></script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
<div style="width: 25%;height: 100%;padding: 25px;margin:auto;border: 5px solid black;margin-top: 130px">
<div style="text-align: center">
<h2> Reset password </h2>
</div>
<div>
<form class="form-horizontal" role="form" method="get">
<div class="form-group">
<p class="col-sm-1"></p>
<p class="col-sm-1"><span class="glyphicon glyphicon-user" style="color: rgb(0, 0, 0); font-size: 20px;top: 7px"></span></p>
<div class="col-sm-9">
<input ng-model="zusername" type="text" class="form-control" id="firstname" placeholder=" Please enter email address ">
</div>
<p class="col-sm-1"></p>
</div>
<div class="form-group">
<p class="col-sm-1"></p>
<p class="col-sm-1"><span class="glyphicon glyphicon-inbox" style="color: rgb(0, 0, 0); font-size: 20px;top: 7px;"></span></p>
<div class="col-sm-5">
<input ng-model="yianzhengma" type="text" class="form-control" id="lastname" placeholder=" Please enter the verification code ">
</div>
<div class="col-sm-4">
<input ng-click="dianjiyzm()" id="btn" type="button" style="width: 100%" class="btn-block form-control btn-primary obtain generate_code" value=" Get verification code " onclick="settime(this)">
</div>
<p class="col-sm-1"></p>
</div>
<div class="form-group">
<p class="col-sm-1"></p>
<p class="col-sm-1"><span class="glyphicon glyphicon-lock" style="color: rgb(0, 0, 0); font-size: 20px;top: 7px"></span></p>
<div class="col-sm-9">
<input ng-model="password1" type="text" class="form-control" id="lastname" placeholder=" Please enter the new password ">
</div>
<p class="col-sm-1"></p>
</div>
<div class="form-group">
<p class="col-sm-1"></p>
<p class="col-sm-1"><span class="glyphicon glyphicon-lock" style="color: rgb(0, 0, 0); font-size: 20px;top: 7px"></span></p>
<div class="col-sm-9">
<input ng-model="password2" type="text" class="form-control" id="lastname" placeholder=" Please enter your new password again ">
</div>
<p class="col-sm-1"></p>
</div>
<div class="form-group">
<p class="col-sm-1"></p>
<div class="col-sm-10">
<input ng-click="mimachongzhi01()" width="100%" class="btn-block btn btn-primary form-control" type="button" value=" Reset ">
</div>
<p class="col-sm-1"></p>
</div>
<div class="form-group">
<h5 style="margin-left: 70%"><a href="/"> Return to login registration </a></h5>
</div>
</form>
</div>
</div>
<script type="text/javascript">
// count down
var countdown=3;
function settime(val) {
if (countdown == 0) {
val.removeAttribute("disabled");
val.value=" Get verification code ";
countdown = 3;
return false;
} else {
val.setAttribute("disabled", true);
val.value=" To resend (" + countdown + ")";
countdown--;
}
setTimeout(function() {
settime(val);
},1000);
}
</script>
</body>
</html>

my js file :

You can put HTML Of js The function of is written here

var app = angular.module('myApp',[])
app.controller('myCtrl',function ($scope) {
$scope.dengru01 = function(){
$.post('/dengru02/',{dyhm:$scope.usernameemail,dmm:$scope.dengrupassword},function (dengruxianshi) {
alert(dengruxianshi)
})
}
$scope.zhuce01 = function () {
$.post('/zhuce02/',{yhm:$scope.username,yx:$scope.Email,mm:$scope.password,yzm:$scope.yianzhengma},function (zhucexianshi) {
alert(zhucexianshi)
})
}
$scope.mimachongzhi01 = function () {
$.post('/mimachongzhi02/',{zyhm:$scope.zusername,zyx:$scope.yianzhengma,zxmm1:$scope.password1,zxmm2:$scope.password2},function (chongzhixianshi) {
alert(chongzhixianshi)
})
}
$scope.dianjiyzm = function () {
$.post("/dianjiyianzhengma/",{czyzm:$scope.yianzhengma},function (czyzm1) {
alert(czyzm1)
})
}
})

 

版权声明:本文为[Empire my love]所创,转载请带上原文链接,感谢。 https://pythonmana.com/2022/134/202205141446235531.html