1. The verification data to the front end
2. Generate HTML tag page
3. Leave the previous entry, clear the password
Create a form class
from django import forms class UserInfo(forms.Form): user = forms.CharField(max_length = 32) age = forms.InterField( ) email = forms.EmailField( )
use
fm = UserInfor({"user":"fu", "age":20, "email":"[email protected]"})
Check whether the data is valid: fm.is_vaild () returns True and False
View all legitimate data ret = fm.cleaned_data () Gets the user, age, email with get value
View illegal data and reasons ret = fm.error ()
form rendering label
1. {{fm.as_p}} render all forms
2. {{fm.user}}, {{fm.age}} rendering specifying form strong controllability, the label attribute specifies the class in the form {{fm.user.label}}
3. rely write cycle
{% for hm in fm %} {{ hm.label }}{{ fm }} {{ hm.error }} {% endfor %}
In view function, the correct check data stored incoming tip of fm = fm.cleaned_data ()
The next time the input has not entered the correct data again
form Tags Add css styles
from django import forms
class UserInfo(forms.Form):
msg = {"required":"改字段不能为空"} user = forms.CharField(max_length = 32)
age = forms.InterField( )
email = forms.EmailField( widget = widgets.TextInput(attrs={'class': 'form-control'}),
label = "邮箱",
error_msg = msg,
initial = "默认值"
)
Add a label here to email btn of css styles, the field is empty when the error is displayed when rendering defaults
hook
from django import forms
from django.core.exceptions import ValidationError
# Login form
class UserInfoForm (forms.Form):
User = forms.charField (MAX_LENGTH = 32 )
password = forms.CharField (MAX_LENGTH = 32 )
In Email = forms.EmailField ()
DEF clean_user (Self):
User = self.cleaned_data. GET ( " the user " )
RET = UserInfo.objects.filter (the user = the user) # found data in the database
IF RET:
return RET
the else :
the raise validateError ( " username does not exist " )