Rendering Forms in Templates
Auto-Rendering with as_p
The as_p shortcut wraps each field and its label in a paragraph tag, giving a quick usable layout.
Example: Auto-Rendering with as_p
The as_p shortcut wraps each field and its label in a paragraph tag, giving a quick usable layout.
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Submit</button>
</form>
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Rendering Fields Manually
Reference individual fields when you need custom HTML around each input, still keeping Django's label and validation.
Note: Manual rendering still needs {{ form.email.errors }} shown somewhere to display validation messages.
Example: Rendering Fields Manually
Reference individual fields when you need custom HTML around each input, still keeping Django's label and validation.
<form method="post">
{% csrf_token %}
<label for="id_email">{{ form.email.label }}</label>
{{ form.email }}
</form>
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Looping Over Form Fields
Looping over {{ form }} renders every field with the same wrapper HTML, useful for consistent styling.
Example: Looping Over Form Fields
Looping over {{ form }} renders every field with the same wrapper HTML, useful for consistent styling.
<form method="post">
{% csrf_token %}
{% for field in form %}
<label for="{{ field.id_for_label }}">{{ field.label }}</label>
{{ field }}
{% endfor %}
</form>
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
- Forgetting {% csrf_token %} inside the <form> tag, which makes every POST submission fail.
- Using {{ form.as_p }} then also manually re-writing every field, producing duplicate inputs.
- Not setting method="post" on the <form> tag, so submitted data never reaches the view as POST.
- {{ form.as_p }}, {{ form.as_table }}, and {{ form.as_ul }} render every field automatically in different wrappers.
- For full control, loop over {{ form }} or reference individual fields like {{ form.email }} plus {{ form.email.label_tag }}.
- Every form template needs {% csrf_token %} inside the <form> tag for POST submissions to succeed.
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: