← Back to Django Course | Chapter 7: Django Forms | Lesson 4 of 11

Rendering Forms in Templates

Rendering a form means turning the Python checklist into real boxes and buttons a person can click and type into.

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.

markup
<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.

markup
<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.

markup
<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. #}
Common Mistakes
  1. Forgetting {% csrf_token %} inside the <form> tag, which makes every POST submission fail.
  2. Using {{ form.as_p }} then also manually re-writing every field, producing duplicate inputs.
  3. Not setting method="post" on the <form> tag, so submitted data never reaches the view as POST.
Chapter Summary
  • {{ 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.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.