Django 登錄注冊(cè)的實(shí)現(xiàn)示例
最近在準(zhǔn)備上線一個(gè)網(wǎng)站(基于django的編程技術(shù)學(xué)習(xí)與外包服務(wù)網(wǎng)站),所以會(huì)將自己的在做這個(gè)項(xiàng)目的過(guò)程中遇到的模塊業(yè)務(wù)以及所涉及到的部分技術(shù)記錄在CSDN平臺(tái)里,一是希望可以幫到有需要的同學(xué),二十以供自己后續(xù)回顧學(xué)習(xí)。
今天要分享的是django的登錄和注冊(cè)頁(yè)面功能,其實(shí)做網(wǎng)頁(yè)登錄和注冊(cè)基本上都是必要的一步啦,那么今天我們就來(lái)了解一下。
登錄注冊(cè)前端Html以及Css我就不細(xì)說(shuō)啦,畢竟我主要是負(fù)責(zé)后端業(yè)務(wù)的,再說(shuō)即使你我不會(huì)前端的內(nèi)容,網(wǎng)上一大堆的登錄注冊(cè)的模板,直接拿來(lái)下載就好了,我這個(gè)登陸注冊(cè)的前端模板就是直接在網(wǎng)站上Copy的,另外說(shuō)到這個(gè)登錄注冊(cè)一般有兩種情況(1:登錄為一個(gè)頁(yè)面,注冊(cè)為一個(gè)頁(yè)面,2:登錄注冊(cè)同是在一個(gè)頁(yè)面)。
我先說(shuō)一下在django框架里我們實(shí)現(xiàn)登錄注冊(cè)的一個(gè)大概流程:
拿到前端的模板,我們更據(jù)需要建立對(duì)應(yīng)的數(shù)據(jù)庫(kù)里的字段,然后回來(lái)前端來(lái)看,比如說(shuō)我們點(diǎn)擊注冊(cè),那么我們把注冊(cè)的按鈕的類(lèi)型設(shè)置為submit,給每一個(gè)注冊(cè)頁(yè)面的輸入框設(shè)置一個(gè)name屬性,然后回到后端來(lái)看,在后端里,我們?cè)诤瘮?shù)中需要寫(xiě)入判斷函數(shù),如果是GET方法則返回注冊(cè)頁(yè)面,如果是POST方法(submit即提交表單),我們則會(huì)獲取我們輸入框的信息,然后我們將其存放在數(shù)據(jù)庫(kù)里即可,同時(shí)回到登錄頁(yè)面,然后輸入框輸入對(duì)應(yīng)的信息,如果輸入的信息(賬號(hào)和密碼)存在在數(shù)據(jù)庫(kù)里,則信息正確進(jìn)入我們的首頁(yè)(指定頁(yè)面)。
前端頁(yè)面如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>DjangoText</title>
<link rel="external nofollow" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/js/all.min.js"></script>
<link rel="stylesheet" rel="external nofollow" />
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
<meta charset="utf-8">
<meta name="viewport"
content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui, viewport-fit=cover">
<meta name="apple-mobile-web-app-capable" content="yes">
<link rel="stylesheet" rel="external nofollow" >
<link rel="stylesheet" rel="external nofollow" >
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link rel="stylesheet" href="/static/css/loginstyle.css" rel="external nofollow" >
</head>
<body>
<!-- partial:index.partial.html -->
<body>
<div class="container">
<div class="content">
<div class="login-container animated fadeInDown" style="animation-delay:.3ms;">
<!-- Login -->
<div class="login justify-content-center" id="login-form">
<h1 class="form-title"><i class="fas fa-user" style="color:#55a0ff;"></i> <br> LOGIN
<hr>
</h1>
<div class="form-container animated fadeIn" style="animation-delay:.7ms;">
<form method="POST">{% csrf_token %}
<label for=""><i class="fas fa-at"></i> Email </label>
<input type="text" name="account" placeholder="Account">
<label for=""><i class="fab fa-slack-hash"></i> Password </label>
<input type="password" name="password" placeholder="Password">
<div class="submit-buttons">
<input type="submit" value="登錄" name="loginsubmit">
<input type="button" value="注冊(cè)" class="btn-register">
</div>
</form>
</div>
</div>
<!-- Login -->
<!-- Register -->
<div class="register justify-content-cente animatedr" style="animation-delay:.3s">
<h1 class="form-title "><i class="fas fa-user-plus" style="color:#57efc4;"></i> <br> REGISTER
<hr>
</h1>
<div class="form-container animated fadeIn" style="animation-delay:.3s;">
<form method="POST" action="/login/" >{% csrf_token %}
<label for=""><i class="fab fa-amilia"></i> Name </label>
<input type="text" name="name" placeholder="Name">
<label for=""><i class="fas fa-at"></i> Account </label>
<input type="text" name="account" placeholder="Account">
<label for=""><i class="fab fa-slack-hash"></i> Password </label>
<input type="password" name="password" placeholder="Password">
<label for=""><i class="fab fa-slack-hash"></i> Confirm Password </label>
<input type="password" name="password_confirmation" placeholder="Password">
<div class="submit-buttons">
<input type="submit" name="registersubmit" value="注冊(cè)" style="background:#55efc4;">
<input type="button" value="登錄" class="btn-login">
</div>
</form>
</div>
</div>
<!-- Register -->
<div class="login animated fadeIn" style="animation-delay:.7s;animation-duration:4s;" id="login-bg"></div>
</div>
</div>
</div>
</body>
<!-- partial -->
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/fontawesome-iconpicker/3.2.0/js/fontawesome-iconpicker.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css'></script>
<script src="/static/js/loginscript.js"></script>
</body>
</html>
自定義404報(bào)錯(cuò)頁(yè)面:
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>404 Page</title>
<link rel="stylesheet" href="/static/css/style_error.css" rel="external nofollow" >
</head>
<body>
<section class="wrapper">
<div class="container">
<div id="scene" class="scene" data-hover-only="false">
<div class="circle" data-depth="1.2"></div>
<div class="one" data-depth="0.9">
<div class="content">
<span class="piece"></span>
<span class="piece"></span>
<span class="piece"></span>
</div>
</div>
<div class="two" data-depth="0.60">
<div class="content">
<span class="piece"></span>
<span class="piece"></span>
<span class="piece"></span>
</div>
</div>
<div class="three" data-depth="0.40">
<div class="content">
<span class="piece"></span>
<span class="piece"></span>
<span class="piece"></span>
</div>
</div>
<p class="p404" data-depth="0.50">Error</p>
<p class="p404" data-depth="0.10">Error</p>
</div>
<div class="text">
<article>
<p>{{ errorMsg }}</p>
<button id="back">返回首頁(yè)</button>
</article>
</div>
</div>
</section>
<script src='/static/js/parallax.min.js'></script>
<script src='/static/js/jquery.min.js'></script>
<script src="/static/js/script.js"></script>
<script>
var back = document.getElementById('back');
back.onclick = function(e){
history.back()
}
</script>
</body>
</html>后端業(yè)務(wù)處理代碼如下:
def login(request):
if request.method == "GET":
return render(request,'login.html')
elif request.POST.get('registersubmit'):
print('nihao 注冊(cè)')
name = request.POST.get('name')
account = request.POST.get('account')
password = request.POST.get('password')
checkpassword = request.POST.get('password_confirmation')
try:
User.objects.get(account=account)
except:
if not name or not account or not password or not checkpassword:
return errorResponse(request, '定義技術(shù):昵稱(chēng)or賬號(hào)or密碼存在空值')
if password != checkpassword:
return errorResponse(request, '定義技術(shù):兩次密碼不吻合')
User.objects.create(name=name, account=account, password=password, checkpassword=checkpassword)
return render(request,"login.html")
elif request.POST.get('loginsubmit'):
print('nihao 登錄')
account1 = request.POST.get('account')
password1 = request.POST.get('password')
try:
User.objects.get(account=account1,password=password1)
except:
return errorResponse(request,'定義技術(shù):輸入信息有誤')數(shù)據(jù)庫(kù):
from django.db import models
class User(models.Model):
name=models.CharField(verbose_name='昵稱(chēng)',max_length=30)
account=models.CharField(verbose_name='賬號(hào)',max_length=30)
password=models.CharField(verbose_name='密碼',max_length=30)
checkpassword=models.CharField(verbose_name='確認(rèn)密碼',max_length=30)效果圖:


自定義404報(bào)錯(cuò)頁(yè)面:

另外說(shuō)一下,我這個(gè)前端的登陸注冊(cè)頁(yè)面是在一個(gè)外網(wǎng)Copy下載的,且登陸注冊(cè)的右側(cè)的那個(gè)圖片是你每次登錄注冊(cè)都會(huì)隨機(jī)生成不同的圖片,所以加載的時(shí)候可能會(huì)出現(xiàn)慢的情況。
到此這篇關(guān)于Django 登錄注冊(cè)的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Django 登錄注冊(cè)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- django如何實(shí)現(xiàn)用戶(hù)的注冊(cè)、登錄、注銷(xiāo)功能
- django authentication 登錄注冊(cè)的實(shí)現(xiàn)示例
- Python基于Django實(shí)現(xiàn)驗(yàn)證碼登錄功能
- 利用django和mysql實(shí)現(xiàn)一個(gè)簡(jiǎn)單的web登錄頁(yè)面
- Django通過(guò)自定義認(rèn)證后端實(shí)現(xiàn)多種登錄方式驗(yàn)證
- Django實(shí)現(xiàn)簡(jiǎn)單登錄的示例代碼
- Django實(shí)現(xiàn)前后端登錄
- Django中使用pillow實(shí)現(xiàn)登錄驗(yàn)證碼功能(帶刷新驗(yàn)證碼功能)
- 給Django Admin添加驗(yàn)證碼和多次登錄嘗試限制的實(shí)現(xiàn)
- 使用Tastypie登錄Django的問(wèn)題解決
相關(guān)文章
python django事務(wù)transaction源碼分析詳解
這篇文章主要介紹了python django事務(wù)transaction源碼分析詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03
強(qiáng)烈推薦好用的python庫(kù)合集(全面總結(jié))
這篇文章主要為大家介紹了強(qiáng)烈推薦非常好用的python庫(kù)合集(全面總結(jié)),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
python實(shí)現(xiàn)無(wú)證書(shū)加密解密實(shí)例
這篇文章主要介紹了python實(shí)現(xiàn)無(wú)證書(shū)加密解密的方法,實(shí)例講述了無(wú)證書(shū)加密解密的原理與具體實(shí)現(xiàn)過(guò)程,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-10-10
django生產(chǎn)環(huán)境搭建(uWSGI+django+nginx+python+MySQL)
本文主要介紹了django生產(chǎn)環(huán)境搭建,主要包括uWSGI+django+nginx+python+MySQL,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
Django REST framework 單元測(cè)試實(shí)例解析
這篇文章主要介紹了Django REST framework 單元測(cè)試實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
python 爬取古詩(shī)文存入mysql數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了python 爬取古詩(shī)文存入mysql數(shù)據(jù)庫(kù)的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01
Python開(kāi)發(fā)工具Pycharm的安裝以及使用步驟總結(jié)
今天給大家?guī)?lái)的是關(guān)于Python開(kāi)發(fā)工具的安裝以及使用的相關(guān)知識(shí),文章圍繞著如何使用和安裝Pycharm展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06
python使用cStringIO實(shí)現(xiàn)臨時(shí)內(nèi)存文件訪問(wèn)的方法
這篇文章主要介紹了python使用cStringIO實(shí)現(xiàn)臨時(shí)內(nèi)存文件訪問(wèn)的方法,涉及Python使用cStringIO模塊操作內(nèi)存的技巧,需要的朋友可以參考下2015-03-03

