liveBook · Manning

Validation

As explained in Django in Action

Validation is the process of checking whether a value satisfies specified correctness or format constraints.

Validation checks whether submitted or stored values meet expected correctness or format rules. In Django, field configuration can add these checks on top of the database representation: an EmailField, for example, stores text while checking that input resembles an email address. section 4.8

For web forms, validation sits between submission and the next response. A browser first uses GET to retrieve the form, then sends the entered values with POST. The view binds request.POST to a form and calls is_valid(). Valid data can be processed and followed by a redirect; invalid data keeps the user on the form page with error information. section 7.1.1

A practical validation flow looks like this:

  • Create an unbound form for a GET request so the user sees empty fields. section 7.1.1
  • Bind submitted values with CommentForm(request.POST) when the request is a POST. section 7.1.1
  • Call form.is_valid(); when it succeeds, read validated values through cleaned_data. section 7.1.1
  • Process valid input and redirect to a confirmation page; otherwise render the form again with its errors. section 7.1.1

Comment submission

A comment view uses one URL for both display and submission. GET creates CommentForm(), while POST creates CommentForm(request.POST). If is_valid() returns true, the view obtains name and comment from cleaned_data, sends the message, and redirects to comment_accepted. If validation fails, the same form-rendering path displays the errors. section 7.1.1

Figure 7.2 Logic flow for a view that handles form submission
figure

Follow the request-flow diagram to see how GET, POST, successful validation, redirects, and invalid-form redisplay fit together. section 7.1.1

How Django in Action covers it

How deep it goes
Explains field-based checks, is_valid(), cleaned_data, error redisplay, and redirects without detailing validation internals.
Examples
Demonstrates EmailField format checking and a RiffMates comment form handled by one GET-and-POST view.
What it assumes
Assumes basic Django views and forms while introducing HTTP GET-versus-POST handling through a practical submission flow.
What you can do after
Apply it via Django forms to accept valid submissions, redisplay invalid input with errors, and redirect after success.
How it connects
Connects model fields, Django forms, HTTP GET, HTTP POST, cleaned data, and redirects.

Also explained in

Compare how 30 books explain validation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.