Django【第12篇】:Django之中間件

自定義驗證規則以及中間件簡單介紹

一、python2和python3中的區別html

複製代碼
對於python2內置的字符串類型有str和unicode
   好比:"abc"是字符串,u"你好"是unicode
   字符串(utf-8/gbk編碼以後值)      unicode
   
對於python3內置的字符串類型有bytes和unicode bytes(utf-8/gbk編碼以後值) 字符串(unicode) python3 中的bytes,就是python2中的字符串 python2 中的字符串,就是python3中的unicode
複製代碼

 

二、數據源沒法時時更新,有兩種方法python

方式一:重構構造方法(推薦)正則表達式

複製代碼
方法一:重構構造方法(推薦)
    class ClassesForm(Form):
    name = fields.CharField(
        required=True,  # 必填字段
        error_messages={"required": "姓名不能爲空!!"},  # 顯示中文錯誤提示
        widget=widgets.TextInput(attrs={"placeholder": "姓名", "class": "form-control"}),  # 自動生成input框
    )
    # 若是直接定義成classteacher_id,,_id 的形式,這樣你添加數據的時候不會時時更新,因此在下面定義一個重寫的方法
    # classteacher_id = fields.ChoiceField(choices= models.UserInfo.objects.filter(ut_id = settings.ROLE_CLASSTEACHER).values_list('id', "username"))

        classteacher_id = fields.ChoiceField(choices=[])
        def __init__(self,*args,**kwargs):   #重寫init方法,時時更新
            super().__init__(*args,**kwargs)   #繼承父類
self.fields["classteacher_id"].choices = models.UserInfo.objects.filter(ut_id = settings.ROLE_CLASSTEACHER).values_list('id', "username") 注意: 要是這樣:fields.ChoiceField(choices=[]) 注意choices裏面傳[(1,"講師"),(2,"班主任"),(3,"管理員")]因此數據庫裏取的時候得用values_list
複製代碼

 

方式二:數據庫

方法二:ModelChoiceField(不推薦),queryset
    from django.forms.models import ModelChoiceField  #先導入
    class ClassForm(Form):
        caption = fields.CharField(error_messages={'required':'班級名稱不能爲空'})
        # headmaster = fields.ChoiceField(choices=[(1,'娜娜',)])
        headmaster_id = ModelChoiceField(queryset=models.UserInfo.objects.filter(ut_id=2))

 

三、Form基本使用django

複製代碼
類
    字段
    is_valid()
    cleaned_data
    errors
    字段參數:
        max_length
        min_length
        validators = [RegexValidators("xxx")]
        
    鉤子函數
        clean_字段名
        注意:
            必須有返回值
            只能拿本身當前字段值
            raise ValidationError("xxx")
    下拉框數據源時時更新
        一、重寫init方法
            先執行父類構造方法
            self.fields["xx"].choices = xxxxx
        二、ModelChoiceField
複製代碼

 

四、用戶登陸session

複製代碼
- form的字段能夠定義正則表達式
            password = fields.CharField(
                required=True,
                min_length=3,
                max_length=18,
                error_messages={
                    'required': '密碼不能爲空',
                    'min_length': '密碼長度不能小於3',
                    'max_length': '密碼長度不能大於18',
                    'invalid': '密碼格式錯誤',
                },
                validators=[RegexValidator('\d+','只能是數字') ]
            )
            注意:error_messages的優先級比validators高
複製代碼

 須要導入的模塊函數

from django.forms import Form
from django.forms import fields
from django.forms import widgets
from django.conf import settings
from django.core.validators import ValidationError
from django.core.validators import RegexValidator

 

 

複製代碼
class LoginForm(Form):
    username = fields.CharField(
        required=True,  #必填字段
        min_length=3,
        max_length=16,
        error_messages={
            "required":"用戶名不能爲空",
            "min_length":"長度不能小於3",
            "max_length":"長度不能大於16"
        },
        widget=widgets.TextInput({"placeholder":"username","class":"form-control"})
    )
    password = fields.CharField(
        required=True,
        min_length=3,
        max_length=16,
        error_messages={
            "required": "密碼不能爲空",
            "min_length": "密碼長度不能小於3",
            "max_length": "密碼長度不能大於16",
            # "invalid":"密碼格式錯誤"
            # error_messages的優先級高,若是寫上"invalid":"密碼格式錯誤"這個就會優先顯示這個錯誤
        },
        widget=widgets.PasswordInput({"placeholder":"password","class":"form-control"}),
        validators=[RegexValidator("\d+","密碼只能是數字")]  #能夠進行正則匹配提示錯誤
    )

    def clean_username(self):
        user = self.cleaned_data["username"]
        is_exits = models.UserInfo.objects.filter(username=user).count()
        if not is_exits:
            raise ValidationError("用戶名和密碼錯誤")
        return user   #必須有return
複製代碼

 

views.py ---------loginpost

複製代碼
def login(request):
    if request.method == "GET":
        form = LoginForm()
        return render(request, "login.html", {"form": form})
    else:
        form = LoginForm(data=request.POST)
        if form.is_valid():
            print(form.cleaned_data)
            # username = form.cleaned_data["username"]
            # password = form.cleaned_data["password"]
            # user = models.UserInfo.objects.filter(username=username, password=password).first()
            user = models.UserInfo.objects.filter(**form.cleaned_data).first()
            if user:  #此次是和數據庫裏的數據進行比較
                #驗證成功
                print(user.username)
                request.session[settings.GDP] = {"id":user.id,"username":user.username}  #設置session
                return redirect("/teacherindex/")
            else:
                #驗證失敗,就給增長一個錯
                form.add_error("password","用戶名或密碼不正確")
                return render(request, "login.html", {"form": form})
        else:
            return render(request, "login.html", {"form": form})
複製代碼

 

- 主動向form中添加錯誤信息
  # form.add_error('password','用戶名或密碼錯誤')
  form.add_error('password',ValidationError('用戶名或密碼錯誤'))
  這兩個均可以,建議用第二個性能

五、Form擴展(鉤子函數)測試

若是對username作擴展
#先作正則表達式判斷
#而後自定義方法驗證:也就是clean_xx,稱爲鉤子函數

複製代碼
def clean_username(self):
        #能夠寫本身的驗證提示
        不像validators只寫正則表達式。在這裏能夠隨意寫
        user=self.clean_data["username"]
        is_esits = models.UserInfo.objects.filter(username=user).count()
        if not is_esits:
            raise validationError("用戶名不存在")
        return user   #必須有返回值
    若是 def clean_username(self):  只能取password字段的值
    若是 def clean_username(self):  只能取username字段的值
    注意:在本身寫鉤子函數的時候,只能拿本身的字段不能拿別人的
    每一種字段就能夠用 正則+自定義正則+自定義鉤子函數
複製代碼

六、中間件

一、中間件是什麼?

中間件顧名思義,是介於request與response處理之間的一道處理過程,相對比較輕量級,而且在全局上改變django的輸入與輸出。由於改變的是全局,因此須要謹慎實用,用很差會影響到性能。

二、作過什麼?
  用戶登陸
  日誌記錄
  crsf:對全部的post請求作了一個驗證
  session
  權限管理

三、

注意:
  對於全部請求的批量作處理的時候用中間件
  單獨對某幾個函數作處理的時候用裝飾器

四、使用步驟:

複製代碼
步驟:
一、、先建一個文件夾,裏面寫一個py文件 二、、而後開始寫類 1.中間件就是一個類,類裏面寫幾個方法 class M1(MiddlewareMixin): 必須繼承 def process_request(self,request): request:請求裏面的全部的東西 print("m1.request_request") 這個方法裏面別輕易返回值,要是有返回值就再也不繼續執行後面的了,執行本身的process_response和上邊的response 通常是無返回值的:繼續執行後續的中間件和視圖函數 def process_response(self,request,response): return response 2.在settings中的MIDDLEWARE加上路徑 文件夾名稱.py文件名稱.類名 3.找到繼承的那個類,吧那個類拿過來 通常不要用導入的方法,否則有時候更新了就沒有這個類了,你就把它繼承的那個類拿過來,
複製代碼

圖示分析過程:

process_reques有返回值:

 

process_reques無返回值:

 

在中間件中設置:

 

 

示例:

複製代碼
class MiddlewareMixin(object):
    def __init__(self, get_response=None):
        self.get_response = get_response
        super(MiddlewareMixin, self).__init__()

    def __call__(self, request):
        response = None
        if hasattr(self, 'process_request'):
            response = self.process_request(request)
        if not response:
            response = self.get_response(request)
        if hasattr(self, 'process_response'):
            response = self.process_response(request, response)
        return response

# 至少要有兩個類
class Md1(MiddlewareMixin):  #必須繼承
    def process_request(self,request):
        print("md1===process_request")
        l = ["/login/"]
        #request.path_info:當前的路徑
        if request.path_info in l:  #由於login不作驗證,就直接返回none就好了
            return None
        if not request.session.get(settings.GDP):
            return redirect("/login/")
        #
        # 若是無返回值,就繼續執行後續中間件和視圖函數
        # 若是有返回值,就執行本身的process_response和上面的response
    def process_response(self,request,response):
        print("md1====process_response1")
        return response   #必須有返回值

class Md2(MiddlewareMixin):
    def process_request(self,request):
        print("md2====process_request2")
    def process_response(self,request,response):
        print("md2====process_response2")
        return response
複製代碼

測試:

def testMD(request):
    print("view.test")
    return HttpResponse("...") 

返回結果:

相關文章
相關標籤/搜索