liveBook · Manning

Cleaned_data

As explained in Django in Action

cleaned_data is a Django form mapping containing the validated, converted value for each form field.

After a Django form passes validation, cleaned_data gives the view a dictionary of field names and their validated, converted values. Treat it as the form’s reliable handoff to application logic: inspect it, build a message, save data, or trigger another action. section 7.1.1 section 7.6

A typical form-submission flow uses cleaned_data like this:

  • Create a bound form with request.POST when the request uses POST. section 7.1.1
  • Call form.is_valid(); only continue when it returns True. section 7.1.1 section 7.6
  • Read values with field-name keys such as form.cleaned_data["name"]. section 7.1.1
  • Use those values in the view’s next operation, then redirect or render a response. section 7.1.1

Reading a submitted comment

if form.is_valid(): name = form.cleaned_data["name"] comment = form.cleaned_data["comment"] message = f"Received comment from {name}\n\n{comment}" send_mail("Received comment", message, "admin@example.com", ["admin@example.com"], fail_silently=False) return redirect("comment_accepted") Here, validation comes first; cleaned_data supplies the name and comment for the email. Invalid submissions stay on the form-rendering path instead of reaching this block. section 7.1.1 section 7.6

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

Use the request-flow diagram to follow the split between displaying a form, validating a POST, processing cleaned_data, and returning a response. section 7.1.1

How Django in Action covers it

How deep it goes
Explains the post-validation dictionary and field lookups, without descending into Django’s validation internals.
Examples
Uses a RiffMates comment form whose cleaned name and comment become an email message.
What you can do after
Apply validated field values in a Django view to construct messages and complete a submission flow.
How it connects
Connects form validation with HTTP POST handling, email sending, and redirects.

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.