Django微講解(十一)
阿新 • • 發佈:2022-05-25
Django微講解(十一)
csrf裝飾器
# 1.針對FBV (1).csrf_protect開啟csrf校驗 程式碼演示: from django.views.decorators.csrf import csrf_exempt,csrf_protect @csrf_protect def login(request): return render(request,'login.html') (2).csrf_exempt忽略csrf校驗 程式碼演示: from django.views.decorators.csrf import csrf_exempt,csrf_protect @csrf_exempt def login(request): return render(request,'login.html') # 2.針對CBV (1).csrf_protect開啟csrf校驗,針對CBV基於新增裝飾器的三種方式都可以生效。 程式碼演示: from django import views from django.utils.decorators import method_decorator # @method_decorator(csrf_protect,name='post') # 可以生效 class MyViews(views.View): # @method_decorator(csrf_protect) # 可以生效 def post(self,request): return HttpResponse('不要大笑,以防樂極生悲') @method_decorator(csrf_protect) # 可以生效 def dispatch(self, request, *args, **kwargs): return super(MyViews,self).dispatch(request, *args, **kwargs) (2).csrf_exempt忽略csrf校驗,針對CBV基於新增裝飾器的三種方式只有一種可以生效。 程式碼演示: from django import views from django.utils.decorators import method_decorator # @method_decorator(csrf_exempt,name='post') # 不會生效 class MyViews(views.View): # @method_decorator(csrf_exempt) # 不會生效 def post(self,request): return HttpResponse('不要大笑,以防樂極生悲') @method_decorator(csrf_exempt) # 可以生效 def dispatch(self, request, *args, **kwargs): return super(MyViews,self).dispatch(request, *args, **kwargs)
基於中介軟體思想編寫專案
# 1.import importlib模組 可以通過字串的形式匯入模組,常規的匯入方式是可以直接指定到變數名的,但是importlib模組不行,最小匯入單位就 是模組檔案。 程式碼演示: import importlib module_path = 'ccc.aaa' res = importlib.import_module(module_path) print(res.name) # 2.傳送提示資訊簡單程式碼演示 import settings import importlib def send_all(msg): # 迴圈獲取配置檔案中字串資訊 for str_path in settings.NOTIFY_FUNC_LIST: # 切割路徑資訊 module_path,class_str_name = str_path.rsplit('.',maxsplit=1) # 根據module_path匯入檔案 module = importlib.import_module(module_path) # 利用反射獲取模組檔案中對應的類名 class_name = getattr(module,class_str_name) # 例項化 obj = class_name() # 呼叫傳送資訊的功能 obj.send(msg)
auth認證模組
auth認證模組是Django提供給我們快速完成使用者相關功能的模組,比如使用者的建立、認證等等,關於auth認證模組Django 也配套提供了一張使用者表,這張表是執行資料庫遷移命令的時候自動生成的auth_user表,Django自帶的admin後臺管理使用者登入參考 的就是這張auth_user表,我們可以建立一個管理員使用者,就可以登入到後臺,點選上方的:Tools-->run manage.py--> createsuperuser,就可以建立管理員使用者。 # 1.驗證使用者和密碼是否正確 auth.authenticate() # 2.儲存使用者登入狀態 auth.login() 程式碼演示: from django.contrib import auth def lg(request): if request.method == 'POST': username = request.POST.get('username') password = request.POST.get('password') is_user_obj = auth.authenticate(request,username=username,password=password) # 先對密碼進行 加密,然後進行比對 # print(is_user_obj) # 校驗正確返回的是使用者物件,錯誤返回None if is_user_obj: # 記錄使用者登入狀態 auth.login(request,is_user_obj) # 自動操作session return render(request,'lg.html') # 3.獲取當前使用者物件 request.user # 4.判斷當前使用者是否登入 request.user.is_authenticated() 程式碼演示: def get_user(request): print(request.user) # 已登入返回的是使用者物件,沒有登入返回AnonymousUser print(request.user.is_authenticated()) # 已登入返回Ture,沒有登入返回False return HttpResponse('檢視使用者是否已登入') # 5.校驗登入裝飾器 from django.contrib.auth.decorators import login_required @login_required(login_url='/lg/') # 區域性配置 @login_required # 全域性配置 LOGIN_URL = '/lg/' # 全域性配置需要在配置檔案中新增配置 程式碼演示: from django.contrib.auth.decorators import login_required # @login_required(login_url='/func/') # 使用者沒有登入預設跳轉到accounts/login/,也可以加括號自定義跳轉頁面 # @login_required(login_url='/func/') # 區域性配置 @login_required # 全域性配置 def index(request): return HttpResponse('登入之後才能檢視的index頁面') # 6.修改密碼 request.user.check_password() request.user.set_password() request.user.save() 程式碼演示: @login_required def set_pwd(request): if request.method == 'POST': old_password = request.POST.get('old_password') new_password = request.POST.get('new_password') is_right = request.user.check_password(old_password) # 自動加密在比對 if is_right: # 修改密碼 request.user.set_password(new_password) # 臨時修改密碼 # 儲存資料 request.user.save() # 將修改的密碼同步到資料庫中 return render(request,'set_pwd.html',locals()) # 7.登出登入 auth.logout(request) 程式碼演示: @login_required def login_out(request): auth.logout(request) return HttpResponse('登出成功') # 8.註冊使用者 from django.contrib.auth.models import User User.objects.create_superuser() # 建立管理員 User.objects.create_user() # 建立普通使用者 程式碼演示: from django.contrib.auth.models import User def register(request): # User.objects.create_user(username='jason',password='123') # 建立普通使用者 User.objects.create_superuser(username='tom',password='123',email='[email protected]') # 建立管理 員,這三個欄位必須填寫 return HttpResponse('註冊')
auth擴充套件表字段
擴充套件auth的表字段有兩種方式,第一種是編寫一對一表關係,這個稍作了解就行,主要是第二種用類繼承來擴充套件auth的
表字段,需要注意的是類繼承之後需要重新執行資料庫遷移命令,並且庫裡面是第一次操作才可以,而且auth模組的所有方法都可
以直接在自定義模型類上面使用,會自動切換參照表。
# 類繼承擴充套件auth表字段
from django.contrib.auth.models import AbstractUser
class MyUser(AbstractUser):
# 編寫AbstractUser類中沒有的欄位,不能衝突
phone = models.BigIntegerField()
AUTH_USER_MODEL = 'app01.Users' # 在配置檔案中新增配置,告訴auth模組,不在使用auth_user表,使用自定義的表
這裡是IT小白陸祿緋,歡迎各位大佬的指點!!!