Templates में Forms Render करना
<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.
<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 रखते हुए।
उदाहरण: 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. #}
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.
<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. #}
- <form> tag के अंदर {% csrf_token %} भूल जाना, जो हर POST submission को fail करा देता है।
- {{ form.as_p }} उपयोग करना फिर हर field को manually फिर से लिखना, duplicate inputs बनाते हुए।
- <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 %} चाहिए।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: