• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

Formal Definition

A Django Form is a Python class that manages the rendering of HTML forms, parsing of submitted data, and rigorous validation of user input before it is processed by the backend.

Explanation

Handling user input securely is hard. You have to write HTML, check if fields are empty, check if emails are valid, and sanitize data to prevent attacks. Django Forms automate all of this. You define the form in Python, and Django generates the HTML and handles the validation logic.

How It Works

  1. Developer defines a forms.Form or forms.ModelForm class.
  2. The view instantiates the form and passes it to the template for rendering.
  3. Upon POST request, the view binds the submitted data to the form (form = MyForm(request.POST)).
  4. The view calls form.is_valid().
  5. Django runs all defined validators (e.g., checking email format, max length).
  6. If valid, data is available in form.cleaned_data. If invalid, errors are attached to the form for the template to display.

Visual Explanation

graph TD
  A[User Submits POST Data] --> B(Bind data to Form Class)
  B --> C{is_valid?}
  C -->|Yes| D[Access form.cleaned_data]
  C -->|No| E[Return form with error messages]

Mental Model & Analogy

A Django Form is like a strict bouncer at an exclusive club. Before anyone (data) gets in, the bouncer checks their ID, checks the guest list, and ensures they meet the dress code (validation). If they fail, they are turned away with a reason. If they pass, they are let in safely.

Implementation & Examples

from django import forms
 
class ContactForm(forms.Form):
    subject = forms.CharField(max_length=100)
    message = forms.CharField(widget=forms.Textarea)
    sender = forms.EmailField()

Key Properties

  • ModelForms automatically generate fields based on a Django Model.
  • Handles HTML rendering (as_p, as_table).
  • Clean methods allow for custom, complex cross-field validation.

Connections

Edge Cases & Gotchas

  • Never trust request.POST data directly; always access validated data via form.cleaned_data.