djangodocs.org

706 sections in Django 6.0 Search all versions →

Topic guide Form handling with class-based views

Here is an example showing how you might go about implementing a form that works with an API-based workflow as well as ‘normal’ form POSTs: The above example assumes that if the client supports text/html, that they would prefer it.

Topic guide Form Assets (the Media class)

Every time the CalendarWidget is used on a form, that form will be directed to include the CSS file pretty.css, and the JavaScript files animations.js and actions.js. This static definition is converted at runtime into a widget property named media.

css

Topic guide Form Assets (the Media class) Assets as a static definition

A dictionary describing the CSS files required for various forms of output media. The values in the dictionary should be a tuple/list of file names. See the section on paths for details of how to specify paths to these files.

js

Topic guide Form Assets (the Media class) Assets as a static definition

A tuple describing the required JavaScript files. See the section on paths for details of how to specify paths to these files.

render()

Reference The Forms API Outputting forms as HTMLDefault rendering

Form.render(template_name=None,context=None,renderer=None) The render method is called by __str__ as well as the Form.as_div(), Form.as_table(), Form.as_p(), and Form.as_ul() methods.

The low-level render API

Reference The form rendering API

The rendering of form templates is controlled by a customizable renderer class. A custom renderer can be specified by updating the FORM_RENDERER setting. It defaults to 'django.forms.renderers.DjangoTemplates'.

DjangoTemplates

Reference The form rendering API Built-in-template form renderers

It loads templates first from the built-in form templates directory in django/forms/templates and then from the installed apps’ templates directories using the app_directories loader.

TemplatesSetting

Reference The form rendering API Built-in-template form renderers

classTemplatesSetting[source] This renderer gives you complete control of how form and widget templates are sourced. It uses get_template() to find templates based on what’s configured in the TEMPLATES setting.

Forms

Reference Settings Core Settings Topical Index

FORM_RENDERER

template_name

Reference The Forms API Outputting forms as HTMLDefault rendering

Form.template_name The name of the template rendered if the form is cast into a string, e.g. via print(form) or in a template via {{ form }}. By default, a property returning the value of the renderer’s form_template_name.

get_context()

Reference The Forms API Outputting forms as HTMLDefault rendering

Form.get_context() Return the template context for rendering the form. The available context is: form: The bound form. fields: All bound fields, except the hidden fields. hidden_fields: All hidden bound fields.

template_name_label

Reference The Forms API Outputting forms as HTMLDefault rendering

Form.template_name_label The template used to render a field’s <label>, used when calling BoundField.label_tag()/legend_tag(). Can be changed per form by overriding this attribute or more generally by overriding the default template, see also Overriding built-in form templates.

as_div()

Reference The Forms API Outputting forms as HTMLOutput styles

Form.template_name_div The template used by as_div(). Default: 'django/forms/div.html'. Form.as_div() as_div() renders the form as a series of <div> elements, with each <div> containing one field, such as: … gives HTML like:

as_p()

Reference The Forms API Outputting forms as HTMLOutput styles

Form.template_name_p The template used by as_p(). Default: 'django/forms/p.html'. Form.as_p() as_p() renders the form as a series of <p> tags, with each <p> containing one field: … gives HTML like:

as_ul()

Reference The Forms API Outputting forms as HTMLOutput styles

Form.template_name_ul The template used by as_ul(). Default: 'django/forms/ul.html'. Form.as_ul() as_ul() renders the form as a series of <li> tags, with each <li> containing one field.

as_table()

Reference The Forms API Outputting forms as HTMLOutput styles

Form.template_name_table The template used by as_table(). Default: 'django/forms/table.html'. Form.as_table() as_table() renders the form as an HTML <table>: … gives HTML like: