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

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

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

上面的 authenticate方法
user = authenticate(username='someone',password='somepassword') 必須要有username和password
4 用戶的登出 logout
def log_out(request):
auth.logout(request) #使用 logout 方法
return redirect("/login/")
5 給用戶增加一個修改密碼的功能
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="舊密碼輸入錯誤"
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>確認新密碼 <input type="text" name="repeatpassword"></div>
<div><input type="submit"> <span>{{ state }}</span></div>
</form>
</body>
</html>
check_password() 驗證用戶輸入的密碼是否和數(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>
未登錄時

嘗試登錄之后

下面修改密碼報錯情況


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

