欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

django框架auth模塊用法實(shí)例詳解

 更新時(shí)間:2019年12月10日 11:20:28   作者:dawn-liu  
這篇文章主要介紹了django框架auth模塊用法,結(jié)合實(shí)例形式詳細(xì)分析了Django框架auth模塊登錄驗(yàn)證、注銷等基本操作技巧與使用注意事項(xiàng),需要的朋友可以參考下

本文實(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)文章

最新評論