django中form元件
阿新 • • 發佈:2018-11-20
Form介紹
我們之前在HTML頁面中利用form表單向後端提交資料時,都會寫一些獲取使用者輸入的標籤並且用form標籤把它們包起來。
與此同時我們在好多場景下都需要對使用者的輸入做校驗,比如校驗使用者是否輸入,輸入的長度和格式等正不正確。如果使用者輸入的內容有錯誤就需要在頁面上相應的位置顯示對應的錯誤資訊.。
在登入,註冊的時候,校驗的程式碼到底是前端來寫還是後端來寫??
其實最好都寫,前端部分使用js來判斷表單的內容是否存在空白,如果有必填項沒有寫,那麼給後端提交請求就增加了伺服器的壓力
而後端要連結資料庫,以及一系列的操作,是不可或缺的,如果校驗的程式碼都寫在了前端上也有問題,因為js可能會被瀏覽器禁用,或者一些爬蟲指令碼會繞過js程式碼,所以後端校驗必須要寫,而前端程式碼最好要寫
這裡的校驗可以通過前端的ajax請求到後端進行校驗,但是django給我們提供了一套便利的元件來供我們使用
Django form元件就實現了上面所述的功能。
其實form元件的主要功能如下:
- 生成頁面可用的HTML標籤
- 對使用者提交的資料進行校驗
- 保留上次輸入內容
普通的方式實現註冊功能
# 註冊 def register(request): error_msg = "" if request.method == "POST": username = request.POST.get("name") pwd= request.POST.get("pwd") # 對註冊資訊做校驗 if len(username) < 6: # 使用者長度小於6位 error_msg = "使用者名稱長度不能小於6位" else: # 將使用者名稱和密碼存到資料庫 return HttpResponse("註冊成功") return render(request, "register.html", {"error_msg": error_msg})
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>註冊頁面</title> </head> <body> <form action="/reg/" method="post"> {% csrf_token %} <p> 使用者名稱: <input type="text" name="name"> </p> <p> 密碼: <input type="password" name="pwd"> </p> <p> <input type="submit" value="註冊"> <p style="color: red">{{ error_msg }}</p> </p> </form> </body> </html>register.html
使用Form元件實現註冊功能
from django import forms # 按照Django form元件的要求自己寫一個類 class RegForm(forms.Form): name = forms.CharField(label="使用者名稱") pwd = forms.CharField(label="密碼")views.py中的form類
# 使用form元件實現註冊方式 def register2(request): form_obj = RegForm() if request.method == "POST": # 例項化form物件的時候,把post提交過來的資料直接傳進去 form_obj = RegForm(request.POST) # 呼叫form_obj校驗資料的方法 if form_obj.is_valid(): return HttpResponse("註冊成功") return render(request, "register2.html", {"form_obj": form_obj})views.py中的檢視函式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>註冊2</title> </head> <body> <form action="/reg2/" method="post" novalidate autocomplete="off"> {% csrf_token %} <div> <label for="{{ form_obj.name.id_for_label }}">{{ form_obj.name.label }}</label> {{ form_obj.name }} {{ form_obj.name.errors.0 }} </div> <div> <label for="{{ form_obj.pwd.id_for_label }}">{{ form_obj.pwd.label }}</label> {{ form_obj.pwd }} {{ form_obj.pwd.errors.0 }} </div> <div> <input type="submit" class="btn btn-success" value="註冊"> </div> </form> </body> </html>register2.html
看網頁效果發現 也驗證了form的功能:
• 前端頁面是form類的物件生成的 -->生成HTML標籤功能
• 當用戶名和密碼輸入為空或輸錯之後 頁面都會提示 -->使用者提交校驗功能
• 當用戶輸錯之後 再次輸入 上次的內容還保留在input框 -->保留上次輸入內容
Form中的常用欄位和外掛
建立Form類時,主要涉及到 【欄位】 和 【外掛】,欄位用於對使用者請求資料的驗證,外掛用於自動生成HTML;
初始值,input框裡面的初始值。 class LoginForm(forms.Form): username = forms.CharField( min_length=8, label="使用者名稱", initial="張三" # 設定預設值 ) pwd = forms.CharField(min_length=6, label="密碼")initial
class LoginForm(forms.Form): username = forms.CharField( min_length=8, label="使用者名稱", initial="張三", error_messages={ "required": "不能為空", "invalid": "格式錯誤", "min_length": "使用者名稱最短8位" } ) pwd = forms.CharField(min_length=6, label="密碼")error_messages
class LoginForm(forms.Form): ... pwd = forms.CharField( min_length=6, label="密碼", widget=forms.widgets.PasswordInput(attrs={'class': 'c1'}, render_value=True) )password
class LoginForm(forms.Form): username = forms.CharField( min_length=8, label="使用者名稱", initial="張三", error_messages={ "required": "不能為空", "invalid": "格式錯誤", "min_length": "使用者名稱最短8位" } ) pwd = forms.CharField(min_length=6, label="密碼") gender = forms.fields.ChoiceField( choices=((1, "男"), (2, "女"), (3, "保密")), label="性別", initial=3, widget=forms.widgets.RadioSelect() )radioSelect
class LoginForm(forms.Form): ... hobby = forms.fields.ChoiceField( choices=((1, "籃球"), (2, "足球"), (3, "雙色球"), ), label="愛好", initial=3, widget=forms.widgets.Select() )單選Select
class LoginForm(forms.Form): ... hobby = forms.fields.MultipleChoiceField( choices=((1, "籃球"), (2, "足球"), (3, "雙色球"), ), label="愛好", initial=[1, 3], widget=forms.widgets.SelectMultiple() )多選Select
class LoginForm(forms.Form): ... keep = forms.fields.ChoiceField( label="是否記住密碼", initial="checked", widget=forms.widgets.CheckboxInput() )單選checkbox
class LoginForm(forms.Form): ... hobby = forms.fields.MultipleChoiceField( choices=((1, "籃球"), (2, "足球"), (3, "雙色球"),), label="愛好", initial=[1, 3], widget=forms.widgets.CheckboxSelectMultiple() )多選checkbox
在使用選擇標籤時,需要注意choices的選項可以配置從資料庫中獲取,但是由於是靜態欄位 獲取的值無法實時更新,需要重寫構造方法從而實現choice實時更新。 方式一: from django.forms import Form from django.forms import widgets from django.forms import fields class MyForm(Form): user = fields.ChoiceField( # choices=((1, '上海'), (2, '北京'),), initial=2, widget=widgets.Select ) def __init__(self, *args, **kwargs): super(MyForm,self).__init__(*args, **kwargs) # self.fields['user'].choices = ((1, '上海'), (2, '北京'),) # 或 self.fields['user'].choices = models.Classes.objects.all().values_list('id','caption') 方式二: from django import forms from django.forms import fields from django.forms import models as form_model class FInfo(forms.Form): authors = form_model.ModelMultipleChoiceField(queryset=models.NNewType.objects.all()) # 多選 # authors = form_model.ModelChoiceField(queryset=models.NNewType.objects.all()) # 單選choice欄位注意事項
Django Form所有內建欄位
Field required=True, 是否允許為空 widget=None, HTML外掛 label=None, 用於生成Label標籤或顯示內容 initial=None, 初始值 help_text='', 幫助資訊(在標籤旁邊顯示) error_messages=None, 錯誤資訊 {'required': '不能為空', 'invalid': '格式錯誤'} validators=[], 自定義驗證規則 localize=False, 是否支援本地化 disabled=False, 是否可以編輯 label_suffix=None Label內容字尾 CharField(Field) max_length=None, 最大長度 min_length=None, 最小長度 strip=True 是否移除使用者輸入空白 IntegerField(Field) max_value=None, 最大值 min_value=None, 最小值 FloatField(IntegerField) ... DecimalField(IntegerField) max_value=None, 最大值 min_value=None, 最小值 max_digits=None, 總長度 decimal_places=None, 小數位長度 BaseTemporalField(Field) input_formats=None 時間格式化 DateField(BaseTemporalField) 格式:2015-09-01 TimeField(BaseTemporalField) 格式:11:12 DateTimeField(BaseTemporalField)格式:2015-09-01 11:12 DurationField(Field) 時間間隔:%d %H:%M:%S.%f ... RegexField(CharField) regex, 自定製正則表示式 max_length=None, 最大長度 min_length=None, 最小長度 error_message=None, 忽略,錯誤資訊使用 error_messages={'invalid': '...'} EmailField(CharField) ... FileField(Field) allow_empty_file=False 是否允許空檔案 ImageField(FileField) ... 注:需要PIL模組,pip3 install Pillow 以上兩個字典使用時,需要注意兩點: - form表單中 enctype="multipart/form-data" - view函式中 obj = MyForm(request.POST, request.FILES) URLField(Field) ... BooleanField(Field) ... NullBooleanField(BooleanField) ... ChoiceField(Field) ... choices=(), 選項,如:choices = ((0,'上海'),(1,'北京'),) required=True, 是否必填 widget=None, 外掛,預設select外掛 label=None, Label內容 initial=None, 初始值 help_text='', 幫助提示 ModelChoiceField(ChoiceField) ... django.forms.models.ModelChoiceField queryset, # 查詢資料庫中的資料 empty_label="---------", # 預設空顯示內容 to_field_name=None, # HTML中value的值對應的欄位 limit_choices_to=None # ModelForm中對queryset二次篩選 ModelMultipleChoiceField(ModelChoiceField) ... django.forms.models.ModelMultipleChoiceField TypedChoiceField(ChoiceField) coerce = lambda val: val 對選中的值進行一次轉換 empty_value= '' 空值的預設值 MultipleChoiceField(ChoiceField) ... TypedMultipleChoiceField(MultipleChoiceField) coerce = lambda val: val 對選中的每一個值進行一次轉換 empty_value= '' 空值的預設值 ComboField(Field) fields=() 使用多個驗證,如下:即驗證最大長度20,又驗證郵箱格式 fields.ComboField(fields=[fields.CharField(max_length=20), fields.EmailField(),]) MultiValueField(Field) PS: 抽象類,子類中可以實現聚合多個字典去匹配一個值,要配合MultiWidget使用 SplitDateTimeField(MultiValueField) input_date_formats=None, 格式列表:['%Y--%m--%d', '%m%d/%Y', '%m/%d/%y'] input_time_formats=None 格式列表:['%H:%M:%S', '%H:%M:%S.%f', '%H:%M'] FilePathField(ChoiceField) 檔案選項,目錄下檔案顯示在頁面中 path, 資料夾路徑 match=None, 正則匹配 recursive=False, 遞迴下面的資料夾 allow_files=True, 允許檔案 allow_folders=False, 允許資料夾 required=True, widget=None, label=None, initial=None, help_text='' GenericIPAddressField protocol='both', both,ipv4,ipv6支援的IP格式 unpack_ipv4=False 解析ipv4地址,如果是::ffff:192.0.2.1時候,可解析為192.0.2.1, PS:protocol必須為both才能啟用 SlugField(CharField) 數字,字母,下劃線,減號(連字元) ... UUIDField(CharField) uuid型別 Django Form內建欄位Django Form內建欄位
欄位校驗
from django.forms import Form from django.forms import widgets from django.forms import fields from django.core.validators import RegexValidator class MyForm(Form): user = fields.CharField( validators=[RegexValidator(r'^[0-9]+$', '請輸入數字'), RegexValidator(r'^159[0-9]+$', '數字必須以159開頭')], )RegexValidator驗證器
import re from django.forms import Form from django.forms import widgets from django.forms import fields from django.core.exceptions import ValidationError # 自定義驗證規則 def mobile_validate(value): mobile_re = re.compile(r'^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$') if not mobile_re.match(value): raise ValidationError('手機號碼格式錯誤') class PublishForm(Form): title = fields.CharField(max_length=20, min_length=5, error_messages={'required': '標題不能為空', 'min_length': '標題最少為5個字元', 'max_length': '標題最多為20個字元'}, widget=widgets.TextInput(attrs={'class': "form-control", 'placeholder': '標題5-20個字元'})) # 使用自定義驗證規則 phone = fields.CharField(validators=[mobile_validate, ], error_messages={'required': '手機不能為空'}, widget=widgets.TextInput(attrs={'class': "form-control", 'placeholder': u'手機號碼'})) email = fields.EmailField(required=False, error_messages={'required': u'郵箱不能為空','invalid': u'郵箱格式錯誤'}, widget=widgets.TextInput(attrs={'class': "form-control", 'placeholder': u'郵箱'}))自定義驗證函式
Hook方法
除了上面兩種方式,我們還可以在Form類中定義鉤子函式,來實現自定義的驗證功能。
我們在Fom類中定義 clean_欄位名() 方法,就能夠實現對特定欄位進行校驗。 舉個例子: class LoginForm(forms.Form): username = forms.CharField( min_length=8, label="使用者名稱", initial="張三", error_messages={ "required": "不能為空", "invalid": "格式錯誤", "min_length": "使用者名稱最短8位" }, widget=forms.widgets.TextInput(attrs={"class": "form-control"}) ) ... # 定義區域性鉤子,用來校驗username欄位 def clean_username(self): value = self.cleaned_data.get("username") if "666" in value: raise ValidationError("光喊666是不行的") else: return value區域性鉤子
class LoginForm(forms.Form): ... password = forms.CharField( min_length=6, label="密碼", widget=forms.widgets.PasswordInput(attrs={'class': 'form-control'}, render_value=True) ) re_password = forms.CharField( min_length=6, label="確認密碼", widget=forms.widgets.PasswordInput(attrs={'class': 'form-control'}, render_value=True) ) ... # 定義全域性的鉤子,用來校驗密碼和確認密碼欄位是否相同 def clean(self): password_value = self.cleaned_data.get('password') re_password_value = self.cleaned_data.get('re_password') if password_value == re_password_value: return self.cleaned_data else: self.add_error('re_password', '兩次密碼不一致') raise ValidationError('兩次密碼不一致')全域性鉤子
應用bootstrap樣式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="x-ua-compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css"> <title>login</title> </head> <body> <div class="container"> <div class="row"> <form action="/login2/" method="post" novalidate class="form-horizontal"> {% csrf_token %} <div class="form-group"> <label for="{{ form_obj.username.id_for_label }}" class="col-md-2 control-label">{{ form_obj.username.label }}</label> <div class="col-md-10"> {{ form_obj.username }} <span class="help-block">{{ form_obj.username.errors.0 }}</span> </div> </div> <div class="form-group"> <label for="{{ form_obj.pwd.id_for_label }}" class="col-md-2 control-label">{{ form_obj.pwd.label }}</label> <div class="col-md-10"> {{ form_obj.pwd }} <span class="help-block">{{ form_obj.pwd.errors.0 }}</span> </div> </div> <div class="form-group"> <label class="col-md-2 control-label">{{ form_obj.gender.label }}</label> <div class="col-md-10"> <div class="radio"> {% for radio in form_obj.gender %} <label for="{{ radio.id_for_label }}"> {{ radio.tag }}{{ radio.choice_label }} </label> {% endfor %} </div> </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <button type="submit" class="btn btn-default">註冊</button> </div> </div> </form> </div> </div> <script src="/static/jquery-3.2.1.min.js"></script> <script src="/static/bootstrap/js/bootstrap.min.js"></script> </body> </html> Django form應用Bootstrap樣式簡單示例View Code