前言
本文主要給大家介紹了關于Django中內置用戶認證的相關內容,分享出來供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧。
認證登陸
在進行用戶登陸驗證的時候,如果是自己寫代碼,就必須要先查詢數(shù)據(jù)庫,看用戶輸入的用戶名是否存在于數(shù)據(jù)庫中;
如果用戶存在于數(shù)據(jù)庫中,然后再驗證用戶輸入的密碼,這樣一來就要自己編寫大量的代碼。
事實上,Django已經(jīng)提供了內置的用戶認證功能。
在使用"python manage.py makemigrationss"和"python manage.py migrate"遷移完成數(shù)據(jù)庫之后
根據(jù)配置文件settings.py中的數(shù)據(jù)庫段生成的數(shù)據(jù)表中已經(jīng)包含了6張進行認證的數(shù)據(jù)表,分別是
進行用戶認證的數(shù)據(jù)表為auth_user
要使用Django自帶的認證功能,首先要導入auth模塊
from django.contrib import auth #導入auth模塊
django.contrib.auth中提供了很多方法,我們常用的有三個方法:
authenticate()
提供了用戶認證,即驗證用戶名以及密碼是否正確,一般需要username和password兩個關鍵字參數(shù)
如果通過認證,authenticate()函數(shù)會返回一個User對象。
authenticate()函數(shù)會在User對象上設置一個屬性標識,這個屬性標識經(jīng)過數(shù)據(jù)庫驗證用戶名及密碼。
當我們試圖登陸一個從數(shù)據(jù)庫中直接取出來不經(jīng)過authenticate()的User對象時會報錯。
使用:
user=authenticate(username="uaername",password="password") login(HttpResponse,user)
這個函數(shù)接受一個HttpRequest對象,以及一個通過authenticate()函數(shù)認證的User對象
login(request)登陸用戶
這個函數(shù)使用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)注銷用戶
這個函數(shù)接受一個HttpResponse對象,無返回值。
當調用該函數(shù)時,當前請求的session信息全部被清除。
即使當前用戶沒有登陸,調用該函數(shù)也不會報錯。
使用:
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視圖函數(shù)可以在settings.py文件中通過LOGIN_URL修改默認值
用戶登陸成功后,會重定向到原來的路徑。
user對象
User對象屬性:username,password為必填項
password用哈希算法保存到數(shù)據(jù)庫中
User對象的方法
is_authenticated()
如果是通過auth函數(shù)返回的真實的User對象,返回值則為True。這個方法檢查用戶是否已經(jīng)通過了認證。
is_authenticated()函數(shù)的返回值為True時,表明用戶成功的通過了認證。
創(chuàng)建用戶
使用create_user輔助函數(shù)創(chuàng)建用戶
from django.contrib.auth.models import Useruser=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): # 如果數(shù)據(jù)庫中已經(jīng)存在這個用戶名 msg="該用戶名已存在" else: new_user=User.objects.create_user(username=username,password=password) #創(chuàng)建新用戶 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): # 到數(shù)據(jù)庫中驗證舊密碼通過 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})總結
以上就是這篇文章的全部內容,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網(wǎng)的支持。
新聞熱點
疑難解答
圖片精選