Django 登錄注冊的實(shí)現(xiàn)示例
最近在準(zhǔn)備上線一個(gè)網(wǎng)站(基于django的編程技術(shù)學(xué)習(xí)與外包服務(wù)網(wǎng)站),所以會將自己的在做這個(gè)項(xiàng)目的過程中遇到的模塊業(yè)務(wù)以及所涉及到的部分技術(shù)記錄在CSDN平臺里,一是希望可以幫到有需要的同學(xué),二十以供自己后續(xù)回顧學(xué)習(xí)。
今天要分享的是django的登錄和注冊頁面功能,其實(shí)做網(wǎng)頁登錄和注冊基本上都是必要的一步啦,那么今天我們就來了解一下。
登錄注冊前端Html以及Css我就不細(xì)說啦,畢竟我主要是負(fù)責(zé)后端業(yè)務(wù)的,再說即使你我不會前端的內(nèi)容,網(wǎng)上一大堆的登錄注冊的模板,直接拿來下載就好了,我這個(gè)登陸注冊的前端模板就是直接在網(wǎng)站上Copy的,另外說到這個(gè)登錄注冊一般有兩種情況(1:登錄為一個(gè)頁面,注冊為一個(gè)頁面,2:登錄注冊同是在一個(gè)頁面)。
我先說一下在django框架里我們實(shí)現(xiàn)登錄注冊的一個(gè)大概流程:
拿到前端的模板,我們更據(jù)需要建立對應(yīng)的數(shù)據(jù)庫里的字段,然后回來前端來看,比如說我們點(diǎn)擊注冊,那么我們把注冊的按鈕的類型設(shè)置為submit,給每一個(gè)注冊頁面的輸入框設(shè)置一個(gè)name屬性,然后回到后端來看,在后端里,我們在函數(shù)中需要寫入判斷函數(shù),如果是GET方法則返回注冊頁面,如果是POST方法(submit即提交表單),我們則會獲取我們輸入框的信息,然后我們將其存放在數(shù)據(jù)庫里即可,同時(shí)回到登錄頁面,然后輸入框輸入對應(yīng)的信息,如果輸入的信息(賬號和密碼)存在在數(shù)據(jù)庫里,則信息正確進(jìn)入我們的首頁(指定頁面)。
前端頁面如下:
<!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="注冊" 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="注冊" 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ò)頁面:
<!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">返回首頁</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 注冊') 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ù):昵稱or賬號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ù)庫:
from django.db import models class User(models.Model): name=models.CharField(verbose_name='昵稱',max_length=30) account=models.CharField(verbose_name='賬號',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ò)頁面:
另外說一下,我這個(gè)前端的登陸注冊頁面是在一個(gè)外網(wǎng)Copy下載的,且登陸注冊的右側(cè)的那個(gè)圖片是你每次登錄注冊都會隨機(jī)生成不同的圖片,所以加載的時(shí)候可能會出現(xiàn)慢的情況。
到此這篇關(guān)于Django 登錄注冊的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Django 登錄注冊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- django如何實(shí)現(xiàn)用戶的注冊、登錄、注銷功能
- django authentication 登錄注冊的實(shí)現(xiàn)示例
- Python基于Django實(shí)現(xiàn)驗(yàn)證碼登錄功能
- 利用django和mysql實(shí)現(xiàn)一個(gè)簡單的web登錄頁面
- Django通過自定義認(rèn)證后端實(shí)現(xiàn)多種登錄方式驗(yàn)證
- Django實(shí)現(xiàn)簡單登錄的示例代碼
- Django實(shí)現(xiàn)前后端登錄
- Django中使用pillow實(shí)現(xiàn)登錄驗(yàn)證碼功能(帶刷新驗(yàn)證碼功能)
- 給Django Admin添加驗(yàn)證碼和多次登錄嘗試限制的實(shí)現(xiàn)
- 使用Tastypie登錄Django的問題解決
相關(guān)文章
python通過tcp發(fā)送xml報(bào)文的方法
今天小編就為大家分享一篇python通過tcp發(fā)送xml報(bào)文的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12python中watchdog文件監(jiān)控與檢測上傳功能
這篇文章主要介紹了python中watchdog文件監(jiān)控與檢測上傳功能,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10Python基于socket實(shí)現(xiàn)TCP客戶端和服務(wù)端
這篇文章主要介紹了Python基于socket實(shí)現(xiàn)的TCP客戶端和服務(wù)端,以及socket實(shí)現(xiàn)的多任務(wù)版TCP服務(wù)端,下面相關(guān)操作需要的小伙伴可以參考一下2022-04-04使用Python的urllib2模塊處理url和圖片的技巧兩則
這篇文章主要介紹了使用Python的urllib2模塊處理url和圖片的兩個(gè)小技巧,分別是獲取帶有中文參數(shù)的url內(nèi)容和獲取遠(yuǎn)程圖片的大小和尺寸,需要的朋友可以參考下2016-02-02python實(shí)現(xiàn)簡易連點(diǎn)器
本文主要介紹了python實(shí)現(xiàn)簡易連點(diǎn)器,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01python 系統(tǒng)調(diào)用的實(shí)例詳解
這篇文章主要介紹了python 系統(tǒng)調(diào)用的實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-07-07Vue的el-scrollbar實(shí)現(xiàn)自定義滾動
本篇文章給大家分享了Vue的el-scrollbar實(shí)現(xiàn)自定義滾動實(shí)現(xiàn)的過程和實(shí)例代碼,對此有需要的朋友可以參考下。2018-05-05Python?數(shù)據(jù)庫操作SQL基礎(chǔ)
在本章節(jié)中,我們將討論?Python?數(shù)據(jù)庫操作的基礎(chǔ)知識,重點(diǎn)關(guān)注?SQL即Structured?Query?Language,結(jié)構(gòu)化查詢語言,SQL?是用于管理關(guān)系型數(shù)據(jù)庫的標(biāo)準(zhǔn)編程語言,可以用來執(zhí)行數(shù)據(jù)定義、數(shù)據(jù)操作和數(shù)據(jù)控制等任務(wù)2023-06-06