Python django框架開發(fā)發(fā)布會簽到系統(tǒng)(web開發(fā))
引言
最近學(xué)習(xí)了蟲師的發(fā)布會簽到系統(tǒng)demo,結(jié)合自己所學(xué)django知識,對demo重新塑造了一下。也是為了練練手,鞏固知識?,F(xiàn)在就分享一下成果~
Django工作流
學(xué)習(xí)django web開發(fā),先來簡單了解一下django的工作機制,請看下圖:

簡單說明:
用戶通過瀏覽器訪問:http://127.0.0.1:8000/index,首先運行的是urlpatterns程序,通過url路由找到對應(yīng)的視圖函數(shù)views.py,視圖函數(shù)處理所有邏輯和數(shù)據(jù),并且將用戶要的數(shù)據(jù)經(jīng)過函數(shù)處理后通過index.html返回給瀏覽器前的用戶看。
詳情流程
從用戶通過瀏覽器訪問→函數(shù)處理→數(shù)據(jù)展示,整個形成一個閉關(guān)。

MVC是眾所周知的模式,即:將應(yīng)用程序分解成三個組成部分:model(模型),view(視圖),和 controller(控制 器)。其中:
M——管理應(yīng)用程序的狀態(tài)(通常存儲到數(shù)據(jù)庫中),并約束改變狀態(tài)的行為(或者叫做“業(yè)務(wù)規(guī)則”)。
C——接受外部用戶的操作,根據(jù)操作訪問模型獲取數(shù)據(jù),并調(diào)用“視圖”顯示這些數(shù)據(jù)。控制器是將“模型”和“視圖”隔離,并成為二者之間的聯(lián)系紐帶。
V——負責(zé)把數(shù)據(jù)格式化后呈現(xiàn)給用戶。
Django也是一個MVC框架。但是在Django中,控制器接受用戶輸入的部分由框架自行處理(C交給用戶),所以 Django 里更關(guān)注的是模型(Model)、模板(Template)和視圖(Views),稱為 MTV模式:
M 代表模型(Model),即數(shù)據(jù)存取層。 該層處理與數(shù)據(jù)相關(guān)的所有事務(wù): 如何存取、如何驗證有效性、包含哪些行為以及數(shù)據(jù)之間的關(guān)系等。
T 代表模板(Template),即表現(xiàn)層。 該層處理與表現(xiàn)相關(guān)的決定: 如何在頁面或其他類型文檔中進行顯示。
V 代表視圖(View),即業(yè)務(wù)邏輯層。 該層包含存取模型及調(diào)取恰當模板的相關(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認證登錄
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頁面。
"""
# 判斷是否記住用戶名
if remember == "on":
# 設(shè)置cookie username *過期時間為1周,按秒計算
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/')
#登錄顯示頁面
def login(request):
'''顯示登陸頁面'''
# 獲取cookie username
if 'username' in request.COOKIES:
username = request.COOKIES['username']
else:
username = ''
return render(request,'index.html',{'username': username})
前端代碼
#首頁
<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>
效果如下

首頁
后端代碼
#主頁 def home(request): return render(request,'home.html')
效果如下

發(fā)布會頁面

嘉賓頁面

總結(jié)
以上所述是小編給大家介紹的Python django框架開發(fā)發(fā)布會簽到系統(tǒng)(web開發(fā)),希望迪大家有所幫助!
相關(guān)文章
Python標準庫之多進程(multiprocessing包)介紹
這篇文章主要介紹了Python標準庫之多進程(multiprocessing包)介紹,本文講解了進程池、共享資源、共享內(nèi)存、Manager等內(nèi)容,需要的朋友可以參考下2014-11-11
pandas DataFrame tail的使用小結(jié)
本文主要介紹了pandas DataFrame tail的使用小結(jié),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-05-05
python使用dlib進行人臉檢測和關(guān)鍵點的示例
這篇文章主要介紹了python使用dlib進行人臉檢測和關(guān)鍵點的示例,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12
多線程爬蟲批量下載pcgame圖片url 保存為xml的實現(xiàn)代碼
用Python寫的多線程爬蟲批量下載pcgame的圖片url并保存為xml格式,主要是邏輯代碼,喜歡的朋友可以測試下2013-01-01

