認證登陸
在進行用戶登陸驗證的時候,如果是自己寫代碼,就必須要先查詢數據庫,看用戶輸入的用戶名是否存在於數據庫中;
如果用戶存在於數據庫中,然后再驗證用戶輸入的密碼,這樣一來就要自己編寫大量的代碼。
事實上,Django已經提供了內置的用戶認證功能。
在使用"python manage.py makemigrationss"和"python manage.py migrate"遷移完成數據庫之后
根據配置文件settings.py中的數據庫段生成的數據表中已經包含了6張進行認證的數據表,分別是
- auth_user
- auth_group
- auth_group_permissions
- auth_permission
- auth_user_groups
- auth_user_user_permissions
進行用戶認證的數據表為auth_user
要使用Django自帶的認證功能,首先要導入auth模塊
from django.contrib import auth #導入auth模塊
django.contrib.auth中提供了很多方法,我們常用的有三個方法:
authenticate()
提供了用戶認證,即驗證用戶名以及密碼是否正確,一般需要username和password兩個關鍵字參數
如果通過認證,authenticate()函數會返回一個User對象。
authenticate()函數會在User對象上設置一個屬性標識,這個屬性標識經過數據庫驗證用戶名及密碼。
當我們試圖登陸一個從數據庫中直接取出來不經過authenticate()的User對象時會報錯。
使用:
user=authenticate(username="uaername",password="password")
login(HttpResponse,user)
這個函數接受一個HttpRequest對象,以及一個通過authenticate()函數認證的User對象
login(request)登陸用戶
這個函數使用Django的session框架給某個已認證的用戶附加上session_id信息。
使用:
from django.shortcuts import render,redirect,HttpResponse
from django.contrib.auth import authenticate,login
def auth_view(request):
username=request.POST.GET("usernmae") # 獲取用戶名
password=request.POST.GET("password") # 獲取用戶的密碼
user=authenticate(username=username,password=password) # 驗證用戶名和密碼,返回用戶對象
if user: # 如果用戶對象存在
login(request,user) # 用戶登陸
return redirect("/index/")
else:
return HttpResponse("用戶名或密碼錯誤")
logout(request)注銷用戶
這個函數接受一個HttpResponse對象,無返回值。
當調用該函數時,當前請求的session信息全部被清除。
即使當前用戶沒有登陸,調用該函數也不會報錯。
使用:
from django.shortcuts import render,redirect,HttpResponse
from django.contrib.auth import authenticate,login,logout
def logout_view(request):
logout(request) # 注銷用戶
return redirect("/index/")
user對象的is_authenticated()
要求:
- 用戶登陸后才能訪問某些頁面
- 如果用戶沒有登陸就訪問本應登陸才能訪問的頁面時會直接跳轉到登陸頁面
- 用戶在登陸頁面登陸后,又會自動跳轉到之前訪問的頁面
方法一:
def view1(request):
if not request.user.is_authenticated():
return redirect("/login/")
方法二:
使用Django的login_requierd()裝飾器
使用:
from django.contrib.auth.decorators import login_required
@login_required
def views(request):
pass
如果用戶沒有登陸,則會跳轉到Django默認的登陸URL的"/accountss/login/"
login視圖函數可以在settings.py文件中通過LOGIN_URL修改默認值
用戶登陸成功后,會重定向到原來的路徑。
user對象
User對象屬性:username,password為必填項
password用哈希算法保存到數據庫中
- is_staff:判斷用戶是否擁有網站的管理權限
- is_active:判斷是否允許用戶登陸,設置為“False”時可以不用刪除用戶來禁止用戶登陸
User對象的方法
is_authenticated()
如果是通過auth函數返回的真實的User對象,返回值則為True。這個方法檢查用戶是否已經通過了認證。
is_authenticated()函數的返回值為True時,表明用戶成功的通過了認證。
創建用戶
使用create_user輔助函數創建用戶
from django.contrib.auth.models import User
user=User.objects.create_user(username="username",password="password")
set_password(password)
使用這個方法來修改密碼
使用:
from django.contrib.auth.models import User
user=User.objects.get(username="username") # 獲取用戶對象
user.set_password(password="password") # 設置對象的密碼
user.save()
check_password(password)
用戶想修改密碼的時候,首先要讓用戶輸入原來的密碼。
如果用戶輸入的舊密碼通過密碼驗證,返回True。
例子一,使用set_password()方法來修改密碼
from django.shortcuts import render,redirect,HttpResponse
from django.contrib.auth.models import User
def create_user(request):
msg=None
if request.method=="POST":
username=request.POST.get("username"," ") # 獲取用戶名,默認為空字符串
password=request.POST.get("password"," ") # 獲取密碼,默認為空字符串
confirm=request.POST.get("confirm_password"," ") # 獲取確認密碼,默認為空字符串
if password == "" or confirm=="" or username=="": # 如果用戶名,密碼或確認密碼為空
msg="用戶名或密碼不能為空"
elif password !=confirm: # 如果密碼與確認密碼不一致
msg="兩次輸入的密碼不一致"
elif User.objects.filter(username=username): # 如果數據庫中已經存在這個用戶名
msg="該用戶名已存在"
else:
new_user=User.objects.create_user(username=username,password=password) #創建新用戶
new_user.save()
return redirect("/index/")
return render(request,"login.html",{"msg":msg})
例子二,使用login_required裝飾器來修改密碼
from django.shortcuts import render,redirect,HttpResponse
from django.contrib.auth import authenticate,login,logout
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
@login_required
def change_passwd(request):
user=request.user # 獲取用戶名
msg=None
if request.method=='POST':
old_password=request.POST.get("old_password","") # 獲取原來的密碼,默認為空字符串
new_password=request.POST.get("new_password","") # 獲取新密碼,默認為空字符串
confirm=request.POST.get("confirm_password","") # 獲取確認密碼,默認為空字符串
if user.check_password(old_password): # 到數據庫中驗證舊密碼通過
if new_password or confirm: # 新密碼或確認密碼為空
msg="新密碼不能為空"
elif new_password != confirm: # 新密碼與確認密碼不一樣
msg="兩次密碼不一致"
else:
user.set_password(new_password) # 修改密碼
user.save()
return redirect("/index/")
else:
msg="舊密碼輸入錯誤"
return render(request,"change_passwd.html",{"msg":msg})
