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

Templates में Forms Render करना

एक form render करने का मतलब है Python checklist को असली boxes और buttons में बदलना जिन्हें एक person click और type कर सके।
Syntax
markup
<form method="post">
  {% csrf_token %}
  {{ form.as_p }}
  <button type="submit">Submit</button>
</form>

{{ form.field_name.label_tag }} {{ form.field_name }} {{ form.field_name.errors }}

as_p से Auto-Rendering

as_p shortcut हर field और इसके label को एक paragraph tag में wrap करता है, एक quick usable layout देते हुए।

उदाहरण: 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. #}

Fields को Manually Render करना

जब आपको हर input के around custom HTML चाहिए तो individual fields reference करें, फिर भी Django का label और validation रखते हुए।

Note: Manual rendering को validation messages दिखाने के लिए अब भी कहीं {{ form.email.errors }} दिखाना चाहिए।

उदाहरण: 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. #}

Form Fields पर Loop करना

{{ form }} पर loop करना हर field को same wrapper HTML के साथ render करता है, consistent styling के लिए उपयोगी।

उदाहरण: 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. #}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. <form> tag के अंदर {% csrf_token %} भूल जाना, जो हर POST submission को fail करा देता है।
  2. {{ form.as_p }} उपयोग करना फिर हर field को manually फिर से लिखना, duplicate inputs बनाते हुए।
  3. <form> tag पर method="post" set न करना, इसलिए submitted data कभी view तक POST के रूप में नहीं पहुंचता।
चैप्टर सारांश
  • {{ form.as_p }}, {{ form.as_table }}, और {{ form.as_ul }} हर field को automatically अलग wrappers में render करते हैं।
  • पूरे control के लिए, {{ form }} पर loop करें या individual fields को {{ form.email }} और {{ form.email.label_tag }} जैसे reference करें।
  • हर form template को POST submissions succeed होने के लिए <form> tag के अंदर {% csrf_token %} चाहिए।

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.