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使用multiprocessing實(shí)現(xiàn)一個(gè)最簡單的分布式作業(yè)調(diào)度系統(tǒng)
mutilprocess像線程一樣管理進(jìn)程,這個(gè)是mutilprocess的核心,他與threading很是相像,對多核CPU的利用率會比threading好的多,通過本文給大家介紹Python使用multiprocessing實(shí)現(xiàn)一個(gè)最簡單的分布式作業(yè)調(diào)度系統(tǒng),需要的朋友參考下2016-03-03Python中eval帶來的潛在風(fēng)險(xiǎn)代碼分析
這篇文章主要介紹了Python中eval帶來的潛在風(fēng)險(xiǎn)代碼分析,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12從安裝到應(yīng)用全面掌握Python與OpenCV的配置與高級功能(最新推薦)
OpenCV的強(qiáng)大功能不僅限于基本的圖像處理,還可以擴(kuò)展到實(shí)時(shí)視頻分析、復(fù)雜的圖像拼接和特征匹配等應(yīng)用場景,這篇文章主要介紹了從安裝到應(yīng)用全面掌握Python與OpenCV的配置與高級功能,需要的朋友可以參考下2024-08-08python中使用iterrows()對dataframe進(jìn)行遍歷的實(shí)例
今天小編就為大家分享一篇python中使用iterrows()對dataframe進(jìn)行遍歷的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06詳解Python中的Numpy、SciPy、MatPlotLib安裝與配置
這篇文章主要介紹了詳解Python中的Numpy、SciPy、MatPlotLib安裝與配置,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11python實(shí)現(xiàn)上傳文件到linux指定目錄的方法
這篇文章主要介紹了python實(shí)現(xiàn)上傳文件到linux指定目錄的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01