djangodocs.org

704 sections in Django dev Search all versions →

Reference The form rendering API

Form templates receive a context from Form.get_context(). By default, forms receive a dictionary with the following values: form: The bound form. fields: All bound fields, except the hidden fields. hidden_fields: All hidden bound fields.

Topic guide Formsets

BaseFormSet has a couple of methods that are closely related to the ManagementForm, total_form_count and initial_form_count. total_form_count returns the total number of forms in this formset.

empty_form

Topic guide Formsets

BaseFormSet provides an additional attribute empty_form which returns a form instance with a prefix of __prefix__ for easier use in dynamic forms with JavaScript.

Reusable form templates

Topic guide Working with forms

The HTML output when rendering a form is itself generated via a template. You can control this by creating an appropriate template file and setting a custom FORM_RENDERER to use that form_template_name site-wide.

Write a minimal form

Tutorial Writing your first Django app, part 4

Let’s update our poll detail template (“polls/detail.html”) from the last tutorial, so that the template contains an HTML <form> element: polls/templates/polls/detail.html A quick rundown: The above template displays a radio button for each question choice.

Reference The Forms API

Form.error_css_class Form.required_css_class It’s pretty common to style form rows and fields that are required or have errors. For example, you might want to present required form rows in bold and highlight errors in red.

Topic guide Working with forms

The distinction between Bound and unbound forms is important: An unbound form has no data associated with it. When rendered to the user, it will be empty or will contain default values.

Reference The Forms API

Form.fields You can access the fields of Form instance from its fields attribute: You can alter the field and BoundField of Form instance to change the way it is presented in the form: Beware not to alter the base_fields attribute …