djangodocs.org

654 sections in Django 4.2 Search all versions →

Writing validators

Reference Validators

A validator is a callable that takes a value and raises a ValidationError if it doesn’t meet some criteria. Validators can be useful for reusing validation logic between different types of fields.

The template

Topic guide Working with forms Building a formBuilding a form in Django

We don’t need to do much in our name.html template: All the form’s fields and their attributes will be unpacked into HTML markup from that {{ form }} by Django’s template language.

Forms

Django documentation

Django provides a rich framework to facilitate the creation of forms and the manipulation of form data.

PasswordInput

Reference Widgets Built-in widgetsWidgets handling input of text

classPasswordInput : - input_type: 'password' - template_name: 'django/forms/widgets/password.html' - Renders as: <input type="password" ...>

Uploading multiple files

Topic guide File Uploads Basic file uploads

If you want to upload multiple files using one form field, create a subclass of the field’s widget and set the allow_multiple_selected attribute on it to True.

Reference The Forms API

Form.errors.get_json_data(escape_html=False) Returns the errors as a dictionary suitable for serializing to JSON. Form.errors.as_json() returns serialized JSON, while this returns the error data before it’s serialized. The escape_html parameter behaves as described in Form.errors.as_json().

Reference The Forms API Outputting forms as HTML

classErrorList(initlist=None,error_class=None,renderer=None) : By default, forms use django.forms.utils.ErrorList to format validation errors. ErrorList is a list like object where initlist is the list of errors. In addition this class has the following attributes and methods.

CharField

Reference Model field reference Field types

The default form widget for this field is a TextInput. CharField has the following extra arguments: CharField.max_length : The maximum length (in characters) of the field. The max_length is enforced at the database level and in Django’s validation using MaxLengthValidator.

validate_max

Topic guide Formsets Validating the number of forms in a formset

If validate_max=True is passed to formset_factory(), validation will also check that the number of forms in the data set, minus those marked for deletion, is less than or equal to max_num.

validate_min

Topic guide Formsets Validating the number of forms in a formset

If validate_min=True is passed to formset_factory(), validation will also check that the number of forms in the data set, minus those marked for deletion, is greater than or equal to min_num.

Topic guide Creating forms from models Inline formsets

When overriding methods on InlineFormSet, you should subclass BaseInlineFormSet rather than BaseModelFormSet. For example, if you want to override clean(): See also Overriding clean() on a ModelFormSet. Then when you create your inline formset, pass in the optional argument formset:

InlineModelAdmin options

Reference The Django admin site InlineModelAdmin objects

The shared features are: form fieldsets fields formfield_overrides exclude filter_horizontal filter_vertical ordering prepopulated_fields get_fieldsets() get_queryset() radio_fields readonly_fields raw_id_fields formfield_for_choice_field() formfield_for_foreignkey() formfield_for_manytomany() has_module_permission() The InlineModelAdmin class adds or customizes: InlineModelAdmin.model : The model which the inline is using. This is required.

Via the Python API

Reference The flatpages app How to add, change and delete flatpages

The flatpage form > used in the admin performs this validation check, and can be imported from > django.contrib.flatpages.forms.FlatpageForm and used in your own > views.

django.contrib.postgres

Reference

This optional module contains model fields and form fields for a number of PostgreSQL specific data types. NOTE: Django is, and will continue to be, a database-agnostic web framework.

Widgets

Reference

TIP: Widgets should not be confused with the form fields. > Form fields deal with the logic of input validation and are used directly > in templates.

Widget

Reference Widgets Base widget classes

`value_omitted_from_data(data,files,name)` : Given `data` and `files` dictionaries and this widget’s name, returns whether or not there’s data or files for the widget. The method’s result affects whether or not a field in a model form [falls back to its default](../../topics/forms/modelforms.md#topics-modelform-save).

choices

Reference Model field reference Field options

[(A, B), (A, B) ...]) to use as choices for this field. If choices are given, they’re enforced by model validation and the default form widget will be a select box with these choices instead of the standard text field.