djangodocs.org

654 sections in Django 4.2 Search all versions →

label_suffix

Reference Form fields Core field arguments

Field.label_suffix The label_suffix argument lets you override the form’s label_suffix on a per-field basis:

help_text

Reference Form fields Core field arguments

If you provide help_text, it will be displayed next to the Field when the Field is rendered by one of the convenience Form methods (e.g., as_ul()). Like the model field’s help_text, this value isn’t HTML-escaped in automatically-generated forms.

localize

Reference Form fields Core field arguments

Field.localize The localize argument enables the localization of form data input, as well as the rendered output. See the format localization documentation for more information.

disabled

Reference Form fields Core field arguments

Field.disabled The disabled boolean argument, when set to True, disables a form field using the disabled HTML attribute so that it won’t be editable by users.

has_changed()

Reference Form fields Checking if the field data has changed

Field.has_changed() The has_changed() method is used to determine if the field value has changed from the initial value. Returns True or False. See the Form.has_changed() documentation for more information.

ChoiceField

Reference Form fields Built-in Field classes

classChoiceField(**kwargs) : - Default widget: Select - Empty value: '' (an empty string) - Normalizes to: A string. - Validates that the given value exists in the list of choices. - Error message keys: required, invalid_choice

ModelChoiceField

Reference Form fields Fields which handle relationships

classModelChoiceField(**kwargs) : - Default widget: Select - Empty value: None - Normalizes to: A model instance. - Validates that the given id exists in the queryset. - Error message keys: required, invalid_choice

Basic forms

Topic guide Form handling with class-based views

Given a contact form: forms.py The view can be constructed using a FormView: views.py Notes: FormView inherits TemplateResponseMixin so template_name can be used here. The default implementation for form_valid() simply redirects to the success_url.

Model forms

Topic guide Form handling with class-based views

In this example: CreateView and UpdateView use myapp/author_form.html DeleteView uses myapp/author_confirm_delete.html If you wish to have separate templates for CreateView and > UpdateView, you can set either > template_name or > template_name_suffix > on your view class.

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_table(), Form.as_p(), and Form.as_ul() methods.

modelform_factory

Reference Model Form Functions

modelform_factory(model,form=ModelForm,fields=None,exclude=None,formfield_callback=None,widgets=None,localized_fields=None,labels=None,help_texts=None,error_messages=None,field_classes=None) : Returns a ModelForm class for the given model. You can optionally pass a form argument to use as a starting point for constructing the ModelForm.

modelformset_factory

Reference Model Form Functions

modelformset_factory(model,form=ModelForm,formfield_callback=None,formset=BaseModelFormSet,extra=1,can_delete=False,can_order=False,max_num=None,fields=None,exclude=None,widgets=None,validate_max=False,localized_fields=None,labels=None,help_texts=None,error_messages=None,min_num=None,validate_min=False,field_classes=None,absolute_max=None,can_delete_extra=True,renderer=None,edit_only=False) : Returns a FormSet class for the given model class.

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'.