Topic guide
Creating forms from models
inlineformset_factory uses modelformset_factory and passes most of its arguments to modelformset_factory. This means you can use the widgets parameter in much the same way as passing it to modelformset_factory. See Specifying widgets to use in the form with widgets above.
dev
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 …
4.2
5.0
5.1
5.2
6.0
Reference
The Forms API
Outputting forms as HTML
Form.default_renderer Specifies the renderer to use for the form. Defaults to None which means to use the default renderer specified by the FORM_RENDERER setting.
4.2
5.0
5.1
5.2
6.0
Reference
The Forms API
Dealing with forms that have FileField and ImageField fields is a little more complicated than a normal form.
4.2
5.0
5.1
5.2
6.0
Topic guide
Creating forms from models
Model formsets
Using the widgets parameter, you can specify a dictionary of values to customize the ModelForm’s widget class for a particular field. This works the same way as the widgets dictionary on the inner Meta class of a ModelForm works:
4.2
5.0
5.1
5.2
6.0
Topic guide
Creating forms from models
Inline formsets
inlineformset_factory uses modelformset_factory and passes most of its arguments to modelformset_factory. This means you can use the widgets parameter in much the same way as passing it to modelformset_factory. See Specifying widgets to use in the form with widgets above.
4.2
5.0
5.1
5.2
6.0
How-to
How to use Django’s CSRF protection
A page makes a POST request via AJAX, and the page does not have an HTML form with a csrf_token that would cause the required CSRF cookie to be sent. Solution: use ensure_csrf_cookie() on the view that sends the page.
dev
How-to
How to use Django’s CSRF protection
Edge cases
A page makes a POST request via AJAX, and the page does not have an HTML form with a csrf_token that would cause the required CSRF cookie to be sent. Solution: use ensure_csrf_cookie() on the view that sends the page.
4.2
5.0
5.1
5.2
6.0
Reference
Form fields
Core field arguments
Field.label The label argument lets you specify the “human-friendly” label for this field. This is used when the Field is displayed in a Form.
Reference
Form fields
Field.label_suffix The label_suffix argument lets you override the form’s label_suffix on a per-field basis:
dev
Reference
Form fields
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.
dev
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.
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.
Reference
Form fields
Field.bound_field_class The bound_field_class attribute allows a per-field override of Form.bound_field_class.
dev
Reference
Form fields
Field.has_changed()[source] 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.
dev
Reference
Form fields
classChoiceField(**kwargs)[source] : - 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
dev
Reference
Form fields
classFilePathField(**kwargs)[source] : - Default widget: Select - Empty value: '' (an empty string) - Normalizes to: A string. - Validates that the selected choice exists in the list of choices. - Error message keys: required, invalid_choice
dev
Reference
Form fields
classModelChoiceField(**kwargs)[source] : - 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
dev
Reference
Form fields
To do this, create a subclass of django.forms.Field. Its only requirements are that it implement a clean() method and that its __init__() method accept the core arguments mentioned above (required, label, initial, widget, help_text).
dev
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.
dev