Forms translate HTTP POST data into validated Python values. Django forms handle rendering, validation, and error display—whether bound to models or standalone.
Rendering in templates
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Save</button>
</form>
Bound vs unbound
Unbound form (GET): empty fields. Bound form (POST): Form(request.POST)—call is_valid() before trusting data.
Important interview questions and answers
- Q: Why csrf_token?
A: Prevents cross-site request forgery—proves POST came from your site. - Q: cleaned_data?
A: Dict of validated, normalized values after is_valid()—use this, not raw POST. - Q: File uploads?
A: Userequest.FILESandenctype="multipart/form-data"on the form.
Self-check
- What makes a form "bound"?
- Where do validated values live after is_valid()?
Pitfall: Forgetting {% csrf_token %} yields 403 on POST—every mutating form needs it unless you use exempt (rare and risky).