models.py
class
userinfo
(models.model)
: name=models.charfield(max_length=32)
pwd=models.charfield(max_length=32)
email=models.emailfield(
)
模板檔案
lang
="en"
>
>
charset
="utf-8"
>
>
titletitle
>
rel=
"stylesheet"
href
="/static/bootstrap-3.3.7-dist/css/bootstrap.css"
>
head
>
>
class
="container"
>
type
="email"
name
="email"
class
="form-control"
>
>
#}
>
class
="text-center"
>
渲染頁碼方式一h1
>
class
="row"
>
class
="col-md-6 col-md-offset-3"
>
action="
" method
="post"
>
}:} >
}span
>
>
}:} >
}span
>
>
}:} >
} }span
>
>
}:} >
}span
>
>
>
提交button
>
form
>
div>
div>
>
class
="text-center"
>
渲染頁碼方式二(for迴圈form物件)h1
>
class
="row"
>
class
="col-md-6 col-md-offset-3"
>
action="
" method
="post"
class
="form-group"
>
}} >
}span
>
>
>
提交button
>
form
>
div>
div>
}#}div>
body
>
html
>
視**件from django.shortcuts import render, httpresponse, redirect
*from django import forms
from django.forms import widgets
from django.core.exceptions import non_field_errors, validationerror
class
regform
(forms.form)
: name = forms.charfield(max_length=10,
min_length=2,
label=
'使用者名稱'
, error_messages=
, widget=widgets.textinput(attrs=))
pwd = forms.charfield(max_length=10,
min_length=2,
label=
'密碼'
, widget=widgets.passwordinput(attrs=))
re_pwd = forms.charfield(max_length=10,
min_length=2,
label=
'確認密碼'
, widget=widgets.passwordinput(attrs=))
email = forms.emailfield(label=
'郵箱'
, error_messages=
, widget=widgets.emailinput(attrs=))
# 區域性鉤子(多了乙個我自己寫校驗規則的方式)
defclean_name
(self)
: name = self.cleaned_data.get(
'name'
) user = userinfo.objects.
filter
(name=name)
.first(
)if user:
raise validationerror(
'使用者已存在'
)else
:if name.startswith(
'sb'):
raise validationerror(
'不能以sb開頭'
)return name
# 全域性鉤子
defclean
(self)
: pwd = self.cleaned_data.get(
'pwd'
) re_pwd = self.cleaned_data.get(
're_pwd'
)if pwd and re_pwd:
if pwd == re_pwd:
return self.cleaned_data
else
:raise validationerror(
'密碼不一致'
)# def form_test(request):
# form沒有的字段不會被校驗,一旦校驗通過,cleaned_data裡面也沒有這個字段
# form有的字段如果不傳,一定校驗不通過
# obj = regform()
# 返回乙個布林型別,true代表裡面所有欄位都校驗通過
# print(obj.is_valid())
# if obj.is_valid():
# 校驗通過的字段放在裡面
# print('cleaned_data:',obj.cleaned_data)
# else:
# print('errors:',obj.errors)
# print(obj.errors.get('name')[0])
# print('errors_type:',type(obj.errors))
# from django.forms.utils import errordict
# return render(request,'form_test.html')
defform_test
(request)
: obj = regform(
)if request.method ==
'post'
: obj = regform(request.post)
if obj.is_valid():
# 把字典打散為關鍵字形式
# return httpresponse('失敗')
return render(request,
'form_test.html'
,locals()
)
django元件 forms元件
針對乙個例項 註冊使用者講解。模型 models.py class userinfo models.model name models.charfield max length 32 pwd models.charfield max length 32 email models.emailfield...
Django元件 forms元件
針對乙個例項 註冊使用者講解。模型 models.py class userinfo models.model name models.charfield max length 32 pwd models.charfield max length 32 email models.emailfield...
django元件的forms元件
一.校驗字段功能 models.py class userinfo models.model name models.charfield max length 32 pwd models.charfield max length 32 email models.emailfield tel mode...