Django Form Rendering

Source: Internet
Author: User

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%}
    • {{Field.field}}

    • field instances in the form class, encapsulated by the BoundField. You can use it to access field properties, such as {% char_field.field.max_length%}.


Django Form Rendering

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.