Django'sFormClass
The form contains various fields (field), each field is a class, and each field contains a widget class that controls the display properties of the HTML element.
Form
All the Form is created as a subclass of Django.forms.Form
Form.is_bound returns whether the current form is bound data
hidden_fields () visible_fields () returns all hidden columns and visible columns, respectively
Widget class
Each field will have a default class based on the field type, or you can manually specify the widget's value to change the default widget class, as follows
Name=forms. Charfield (label= ' name ' max_length=200,widget=forms. TextInput (attrs={' class ': ' Form-control '))
Widgets. Attrs
A dictionary of HTML attributes #{' class ': ' Form-control ', ' required ': ' Required '}
data for Fields
The validated form data is located in the Form.cleaned_data dictionary, regardless of the data submitted by the form, once the validation is successfully validated by calling Is_valid () (Is_valid () returns True. This data has been converted for you into Python type.
Note
At this point, you can still get the request. The post has direct access to unverified data, but it is better to access the validated data.
manually render fields
Add Non_field_errors to this error message when manually rendering.
{{Form.non_field_errors}}
<div class= "Fieldwrapper" > {{form.subject.errors}} <label for= "{{Form.subject.id_for_label}}" >email subject:</label> {{Form.subject}}</div>
Useful Properties in field
{{Field.label}}
The label of the field, such as email address.
{{Field.label_tag}}
The field label that is contained in the HTML <label> tag. It contains the label_suffix of the form. For example, the default label_suffix is a colon:
<label for= "Id_email" >email address:</label>
{{Field.id_for_label}}
The ID used for this field (in the example above is Id_email). If you are constructing a label by hand, you may want to use it instead of Label_tag. This is also useful if you have some inline JavaScript and want to avoid hard-coded field IDs.
{{Field.value}}
The value of the field, for example [email protected].
{{Field.html_name}}
The name that will be used in the INPUT element's Name property. It will take into account the form's prefix.
{{Field.help_text}}
The help document associated with the field.
{{Field.errors}}
Output a <ul class= "Errorlist", containing validation error information for this field. You can use {% for error in field.errors%} to customize the display of errors. In this case, each object in the loop is simply a simple string containing the error message.
{{Field.is_hidden}}
True if the field is a hidden field, otherwise false. As a template variable, it is not very useful, but can be used for conditional testing, for example:
{% if Field.is_hidden%} {% ENDIF%}
Django Form Rendering