djangodocs.org

669 sections in Django 5.1 Search all versions →

How errors are displayed

Reference The Forms API Outputting forms as HTML

If you render a bound Form object, the act of rendering will automatically run the form’s validation if it hasn’t already happened, and the HTML output will include the validation errors as a <ul class="errorlist"> near the field.

Reference

Since the field validation methods have been run by the time clean() is called, you also have access to the form’s errors attribute which contains all the errors raised by cleaning of individual fields.

Raising ValidationError

Reference Form and field validation

… coerce variables into the message; use placeholders and the params argument of the constructor: Use mapping keys instead of positional formatting. This enables putting the variables in any order or omitting them altogether when rewriting the message: Wrap the message …

ValidationError

Reference Django Exceptions Django Core Exceptions

exceptionValidationError[source] : The ValidationError exception is raised when data fails form or model field validation. For more information about validation, see Form and Field Validation, Model Field Validation and the Validator Reference.

Raising multiple errors

Reference Form and field validation Raising ValidationError

If you detect multiple errors during a cleaning method and wish to signal all of them to the form submitter, it is possible to pass a list of errors to the ValidationError constructor.

Reference The Forms API

Form.clean() Implement a clean() method on your Form when you must add custom validation for fields that are interdependent. See Cleaning and validating fields that depend on each other for example usage.

Reference The Forms API

Form.cleaned_data Each field in a Form class is responsible not only for validating data, but also for “cleaning” it – normalizing it to a consistent format.

Forms

Reference

Detailed form API reference. For introductory material, see the Working with forms topic guide.

Working with forms

Topic guide

NOTE: This document provides an introduction to the basics of web forms and how > they are handled in Django.

Further topics

Topic guide Working with forms

This covers the basics, but forms can do a whole lot more: Formsets Using initial data with a formset Limiting the maximum number of forms Limiting the maximum number of instantiated forms Formset validation Validating the number of forms in …

… template system Removing hardcoded URLs in templates Namespacing URL names Writing your first Django app, part 4 Write a minimal form Use generic views: Less code is better Writing your first Django app, part 5 Introducing automated testing Basic testing …

Form fields

Reference

classField[source] When you create a Form class, the most important part is defining the fields of the form. Each field has custom validation logic, along with a few other hooks.