django框架auth模塊用法實(shí)例詳解
本文實(shí)例講述了django框架auth模塊用法。分享給大家供大家參考,具體如下:
auth模塊的導(dǎo)入
from django.contrib import auth
django中的auth模塊有其自己完整的一套方法: 登錄驗(yàn)證、注銷、用戶的創(chuàng)建、比較用戶輸入的密碼是否和數(shù)據(jù)庫的一致、用戶信息的修改
1 我們來生成db.sqlite3 (migrations,migrate),打開,從中我們可以找到表 auth_user ,整篇都是圍繞這個(gè)表進(jìn)行的
2 這個(gè)表里面暫時(shí)是空的,我們可以創(chuàng)建 ,例如:創(chuàng)建一個(gè)超級用戶

我們從表 auth_user 中可以看到生成了一條記錄,里面的密碼是經(jīng)過加密的

3 創(chuàng)建一個(gè)登錄視圖和模板

上面的 authenticate方法
user = authenticate(username='someone',password='somepassword') 必須要有username和password
4 用戶的登出 logout
def log_out(request):
auth.logout(request) #使用 logout 方法
return redirect("/login/")
5 給用戶增加一個(gè)修改密碼的功能
def set_password(request):
user=request.user
state=""
if request.method=="POST":
oldpassword=request.POST.get('oldpassword','')
newpassword=request.POST.get('newpassword','')
repeatpassword=request.POST.get('repeatpassword','')
if user.check_password(oldpassword):
if not newpassword:
state="新密碼不能為空"
elif newpassword != repeatpassword:
state="重置的密碼前后不一致"
else:
user.set_password(newpassword)
user.save()
return redirect("/login/")
else:
state="舊密碼輸入錯(cuò)誤"
return render(request,"set_password.html",{"state":state})
#模板
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>修改密碼</title>
</head>
<body>
<form action="" method="post">
{% csrf_token %}
<div>用戶:{{ user }}</div>
<div>舊密碼 <input type="text" name="oldpassword"></div>
<div>新密碼 <input type="text" name="newpassword"></div>
<div>確認(rèn)新密碼 <input type="text" name="repeatpassword"></div>
<div><input type="submit"> <span>{{ state }}</span></div>
</form>
</body>
</html>
check_password() 驗(yàn)證用戶輸入的密碼是否和數(shù)據(jù)庫中的一致 ,一致返回True,否則返回None
6 模擬登錄將index作為首頁,根據(jù)用戶的登錄與否選擇不同的頁面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>index</h1>
<p>hello {{ user }}</p>
{% if request.user.is_authenticated %}
<a href="/logout/" rel="external nofollow" >注銷</a>
<a href="/set_password/" rel="external nofollow" >修改密碼</a>
{% else %}
<div><span>未登錄</span></div>
<p><a href="/login/" rel="external nofollow" >登陸</a></p>
<p><a href="/reg/" rel="external nofollow" >注冊</a></p>
{% endif %}
</body>
</html>
未登錄時(shí)

嘗試登錄之后

下面修改密碼報(bào)錯(cuò)情況


總結(jié):
導(dǎo)入:from django.contrib import auth
驗(yàn)證用戶登錄:user = authenticate(username='someone',password='somepassword')驗(yàn)證成功返回user對象,否則返回none
session的寫操作: auth.login(request,user) #session的寫操作 對應(yīng)于django_session表
用戶的登出或者注銷:auth.logout(request)
驗(yàn)證用戶是否已經(jīng)登錄:# user=request.user # if not user.is_authenticated(): return redirect("/login/")
驗(yàn)證用戶輸入的密碼是否與數(shù)據(jù)庫一致:
user=request.user user.check_password(oldpassword) 成功返回True,否則為None
修改密碼:
user = User.objects.get(username='') #先獲得user對象 user.set_password(password='') user.save
創(chuàng)建用戶,必須要有兩個(gè)信息,用戶名和密碼
from django.contrib.auth.models import User user = User.objects.create_user(username='',password='',email='')
希望本文所述對大家基于Django框架的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python庫textract提取各種文檔類型中文本數(shù)據(jù)
Python的textract庫是一個(gè)強(qiáng)大的工具,它可以從各種文檔類型中提取文本數(shù)據(jù),無論是PDF、Word文檔、圖片還是其他格式的文件,textract都可以輕松地將文本提取出來,本文將詳細(xì)介紹textract的功能和用法,并提供豐富的示例代碼來幫助大家深入了解2024-01-01
Django中使用Celery執(zhí)行定時(shí)任務(wù)問題
這篇文章主要介紹了Django中使用Celery執(zhí)行定時(shí)任務(wù)問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
Pandas時(shí)間序列基礎(chǔ)詳解(轉(zhuǎn)換,索引,切片)
今天小編就為大家分享一篇Pandas時(shí)間序列基礎(chǔ)詳解(轉(zhuǎn)換,索引,切片),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
Python遍歷pandas數(shù)據(jù)方法總結(jié)
本篇文章給大家詳細(xì)介紹了Python中遍歷pandas數(shù)據(jù)方法以及相關(guān)注意點(diǎn),對此有興趣的朋友參考學(xué)習(xí)下吧。2018-02-02
python 非線性規(guī)劃方式(scipy.optimize.minimize)
今天小編就為大家分享一篇python 非線性規(guī)劃方式(scipy.optimize.minimize),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
Django:使用filter的pk進(jìn)行多值查詢操作
這篇文章主要介紹了Django:使用filter的pk進(jìn)行多值查詢操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
簡單掌握Python中g(shù)lob模塊查找文件路徑的用法
glob模塊遵循Unix的shell規(guī)則來匹配文件名進(jìn)行文件查找,下面我們結(jié)合匹配相關(guān)的字符區(qū)間與通配符知識(shí),來簡單掌握Python中g(shù)lob模塊查找文件路徑的用法2016-07-07

