Python django框架開發(fā)發(fā)布會(huì)簽到系統(tǒng)(web開發(fā))
引言
最近學(xué)習(xí)了蟲師的發(fā)布會(huì)簽到系統(tǒng)demo,結(jié)合自己所學(xué)django知識(shí),對(duì)demo重新塑造了一下。也是為了練練手,鞏固知識(shí)?,F(xiàn)在就分享一下成果~
Django工作流
學(xué)習(xí)django web開發(fā),先來(lái)簡(jiǎn)單了解一下django的工作機(jī)制,請(qǐng)看下圖:
簡(jiǎn)單說明:
用戶通過瀏覽器訪問:http://127.0.0.1:8000/index,首先運(yùn)行的是urlpatterns程序,通過url路由找到對(duì)應(yīng)的視圖函數(shù)views.py,視圖函數(shù)處理所有邏輯和數(shù)據(jù),并且將用戶要的數(shù)據(jù)經(jīng)過函數(shù)處理后通過index.html返回給瀏覽器前的用戶看。
詳情流程
從用戶通過瀏覽器訪問→函數(shù)處理→數(shù)據(jù)展示,整個(gè)形成一個(gè)閉關(guān)。
MVC是眾所周知的模式,即:將應(yīng)用程序分解成三個(gè)組成部分:model(模型),view(視圖),和 controller(控制 器)。其中:
M——管理應(yīng)用程序的狀態(tài)(通常存儲(chǔ)到數(shù)據(jù)庫(kù)中),并約束改變狀態(tài)的行為(或者叫做“業(yè)務(wù)規(guī)則”)。
C——接受外部用戶的操作,根據(jù)操作訪問模型獲取數(shù)據(jù),并調(diào)用“視圖”顯示這些數(shù)據(jù)。控制器是將“模型”和“視圖”隔離,并成為二者之間的聯(lián)系紐帶。
V——負(fù)責(zé)把數(shù)據(jù)格式化后呈現(xiàn)給用戶。
Django也是一個(gè)MVC框架。但是在Django中,控制器接受用戶輸入的部分由框架自行處理(C交給用戶),所以 Django 里更關(guān)注的是模型(Model)、模板(Template)和視圖(Views),稱為 MTV模式:
M 代表模型(Model),即數(shù)據(jù)存取層。 該層處理與數(shù)據(jù)相關(guān)的所有事務(wù): 如何存取、如何驗(yàn)證有效性、包含哪些行為以及數(shù)據(jù)之間的關(guān)系等。
T 代表模板(Template),即表現(xiàn)層。 該層處理與表現(xiàn)相關(guān)的決定: 如何在頁(yè)面或其他類型文檔中進(jìn)行顯示。
V 代表視圖(View),即業(yè)務(wù)邏輯層。 該層包含存取模型及調(diào)取恰當(dāng)模板的相關(guān)邏輯。 你可以把它看作模型與模板之間的橋梁。
登錄
后端代碼:
#登錄邏輯處理函數(shù) def login_action(request): if request.method == "POST": username = request.POST.get('username','') password = request.POST.get('password','') remember = request.POST.get('remember','') print(remember,111) #if username == 'admin' and password == '123456': #django認(rèn)證登錄 user = auth.authenticate(username=username,password=password) # print("user:%s"%user) if user is not None: auth.login(request,user) #登陸 #response.set_cookie('user',username,3600) #添加瀏覽器cookie request.session['user'] = username #寫入session 寫入瀏覽器,存入服務(wù)器。 response = HttpResponseRedirect('/home/') """ 重定向,先post→get通過路由urls,找到event_manager函數(shù),跳轉(zhuǎn)到找到event_manager.html頁(yè)面。 """ # 判斷是否記住用戶名 if remember == "on": # 設(shè)置cookie username *過期時(shí)間為1周,按秒計(jì)算 response.set_cookie('username', username, max_age=7 * 24 * 3600) return response else: # return render(request,'index.html',{'error':'username or password error!'}) return redirect('/login/')
#登錄顯示頁(yè)面 def login(request): '''顯示登陸頁(yè)面''' # 獲取cookie username if 'username' in request.COOKIES: username = request.COOKIES['username'] else: username = '' return render(request,'index.html',{'username': username})
前端代碼
#首頁(yè) <html> <head> {% load bootstrap3 %} {% bootstrap_css %} <link rel="stylesheet" href="/static/css/style.css"> </head> <body style="margin: 5%;"> <div class="container"> <div class="form row"> <div class="form-horizontal col-md-offset-3" id="login_form"> <h3 class="form-title" style="padding-left: 20%"><font color="#fffaf0">歡迎登錄</font></h3> <div class="col-md-9"> <form action="/login_action/" method="post"> <div class="form-group"> <i class="fa fa-user fa-lg"></i> <input class="form-control required" type="text" value="{{ username }}" placeholder="Username" id="username" name="username" autofocus="autofocus" maxlength="20"/> </div> <div class="form-group"> <i class="fa fa-lock fa-lg"></i> <input class="form-control required" type="password" placeholder="Password" id="password" name="password" maxlength="8"/> </div> <div class="form-group"> <label class="checkbox"> {# <input type="checkbox" name="remember" value="1"/>記住我#} <input type="checkbox" name="remember"/>記住我 </label> <p>{{ back_dict }}</p> </div> <div class="form-group col-md-offset-9"> <button type="submit" class="btn btn-success pull-right" name="submit">登錄</button> </div> </form> </div> </div> </div> </div> </body> </html>
效果如下
首頁(yè)
后端代碼
#主頁(yè) def home(request): return render(request,'home.html')
效果如下
發(fā)布會(huì)頁(yè)面
嘉賓頁(yè)面
總結(jié)
以上所述是小編給大家介紹的Python django框架開發(fā)發(fā)布會(huì)簽到系統(tǒng)(web開發(fā)),希望迪大家有所幫助!
- 哪些是python中web開發(fā)框架
- Python用Bottle輕量級(jí)框架進(jìn)行Web開發(fā)
- 10款最好的Web開發(fā)的 Python 框架
- 全面解讀Python Web開發(fā)框架Django
- Python用來(lái)做Web開發(fā)的優(yōu)勢(shì)有哪些
- python GUI庫(kù)圖形界面開發(fā)之PyQt5中QWebEngineView內(nèi)嵌網(wǎng)頁(yè)與Python的數(shù)據(jù)交互傳參詳細(xì)方法實(shí)例
- python GUI庫(kù)圖形界面開發(fā)之PyQt5瀏覽器控件QWebEngineView詳細(xì)使用方法
- python Django的web開發(fā)實(shí)例(入門)
- python Web開發(fā)你要理解的WSGI & uwsgi詳解
- 推薦值得學(xué)習(xí)的12款python-web開發(fā)框架
相關(guān)文章
TensorFlow實(shí)現(xiàn)模型斷點(diǎn)訓(xùn)練,checkpoint模型載入方式
這篇文章主要介紹了TensorFlow實(shí)現(xiàn)模型斷點(diǎn)訓(xùn)練,checkpoint模型載入方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-05-05python提取照片坐標(biāo)信息的實(shí)例代碼
這篇文章主要介紹了python提取照片坐標(biāo)信息的實(shí)例代碼,文中給大家提到了Python利用exifread庫(kù)來(lái)解析照片的經(jīng)緯度,通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08python簡(jiǎn)單實(shí)現(xiàn)整數(shù)反轉(zhuǎn)的畫解算法
這篇文章主要介紹了python簡(jiǎn)單實(shí)現(xiàn)整數(shù)反轉(zhuǎn)采用了一個(gè)有趣的畫解算法,通過示例的題目描述來(lái)對(duì)問題分析進(jìn)行方案的解決,有需要的朋友可以參考下2021-08-08Python使用plt庫(kù)實(shí)現(xiàn)繪制動(dòng)態(tài)曲線圖并導(dǎo)出為GIF或MP4
這篇文章主要為大家詳細(xì)介紹了Python如何使用plt庫(kù)實(shí)現(xiàn)繪制動(dòng)態(tài)曲線圖并導(dǎo)出為GIF或MP4,文中的示例代碼講解詳細(xì),需要的可以了解一下2024-03-03Python import導(dǎo)入上級(jí)目錄文件的方法
這篇文章主要介紹了Python import導(dǎo)入上級(jí)目錄文件,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-01-01