日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

django之auth模块

發布時間:2023/12/2 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 django之auth模块 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
django之auth模塊

auth模塊的導入

from django.contrib import auth

django中的auth模塊有其自己完整的一套方法: 登錄驗證、注銷、用戶的創建、比較用戶輸入的密碼是否和數據庫的一致、用戶信息的修改

1 我們來生成db.sqlite3 (migrations,migrate),打開,從中我們可以找到表 auth_user ,整篇都是圍繞這個表進行的

2 ?這個表里面暫時是空的,我們可以創建 ,例如:創建一個超級用戶 ?

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

3 創建一個登錄視圖和模板

上面的 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.userstate=""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() 驗證用戶輸入的密碼是否和數據庫中的一致 ,一致返回True,否則返回None

6 模擬登錄將index作為首頁,根據用戶的登錄與否選擇不同的頁面

<!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/">注銷</a><a href="/set_password/">修改密碼</a> {% else %}<div><span>未登錄</span></div><p><a href="/login/">登陸</a></p><p><a href="/reg/">注冊</a></p> {% endif %} </body> </html>

未登錄時

嘗試登錄之后

下面修改密碼報錯情況

?總結:

導入:from django.contrib import auth

驗證用戶登錄:user?=?authenticate(username='someone',password='somepassword')?驗證成功返回user對象,否則返回none

session的寫操作: ? ? ?auth.login(request,user) #session的寫操作 對應于django_session表

用戶的登出或者注銷:auth.logout(request)

驗證用戶是否已經登錄:# user=request.user ??# if not user.is_authenticated(): return redirect("/login/")

驗證用戶輸入的密碼是否與數據庫一致:

user=request.user user.check_password(oldpassword) 成功返回True,否則為None

?修改密碼:

user = User.objects.get(username='') #先獲得user對象 user.set_password(password='') user.save 

創建用戶,必須要有兩個信息,用戶名和密碼

from django.contrib.auth.models import User user = User.objects.create_user(username='',password='',email='')

?

?







?

posted on 2019-03-18 15:52 dawn-liu 閱讀(...) 評論(...) 編輯 收藏

轉載于:https://www.cnblogs.com/mmyy-blog/p/10552597.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的django之auth模块的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。