Tutorial Writing your first Django app, part 4
The above code checks for KeyError and redisplays the question form with an error message if choice isn’t given. F("votes") + 1 instructs the database to increase the vote count by 1.
669 sections in Django 5.1 Search all versions →
Tutorial Writing your first Django app, part 4
The above code checks for KeyError and redisplays the question form with an error message if choice isn’t given. F("votes") + 1 instructs the database to increase the vote count by 1.
Tutorial Writing your first Django app, part 7
By registering the Question model with admin.site.register(Question), Django was able to construct a default form representation. Often, you’ll want to customize how the admin form looks and works.
Reference
All of these fields and widgets are available from the django.contrib.postgres.forms module.
Reference The Forms API
Form.has_changed() Use the has_changed() method on your Form when you need to check if the form data has been changed from the initial data.
Reference The Forms API Outputting forms as HTML
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
Topic guide Working with forms Building a form
Topic guide Working with forms
All form classes are created as subclasses of either django.forms.Form or django.forms.ModelForm. You can think of ModelForm as a subclass of Form.
Topic guide Working with forms More about Django Form classes
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.
Topic guide Working with forms
All you need to do to get your form into a template is to place the form instance into the template context.
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 …
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.
Reference The Forms API
Dealing with forms that have FileField and ImageField fields is a little more complicated than a normal form.
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:
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.
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.
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 Core field arguments
Field.label_suffix The label_suffix argument lets you override the form’s label_suffix on a per-field basis:
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.
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.