Django學習【第16篇】:Django之Form組件自定義驗證規則


1、python2和python3中的區別

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

 

2、數據源無法時時更新,有兩種方法

方式一:重構構造方法(推薦)

復制代碼
方法一:重構構造方法(推薦)
    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))

 

3、Form基本使用

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

 

4、用戶登錄

復制代碼
- 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 ---------login

復制代碼
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('用戶名或密碼錯誤'))
  這兩個都可以,建議用第二個

5、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字段的值 注意:在自己寫鈎子函數的時候,只能拿自己的字段不能拿別人的 每一種字段就可以用 正則+自定義正則+自定義鈎子函數
復制代碼

6、中間件

1、中間件是什么?

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

2、做過什么?
  用戶登錄
  日志記錄
  crsf:對所有的post請求做了一個驗證
  session
  權限管理

3、

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

4、使用步驟:

復制代碼
步驟:
1、、先建一個文件夾,里面寫一個py文件 2、、然后開始寫類 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("...") 

返回結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM