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

Django Forms का परिचय

एक Django Form कंप्यूटर द्वारा आपके लिए बनाए गए एक paper checklist जैसा है, ताकि इसे भरने वाला person कोई question skip या nonsense type न कर सके।
Syntax
markup
from django import forms

class FormName(forms.Form):
    field_name = forms.CharField()

def view_name(request):
    form = FormName()
    return render(request, 'template.html', {'form': form})

Django Forms क्यों उपयोग करें

Django Forms Python classes हैं जो describe करती हैं कि एक form में कौन सी fields हैं, automatically HTML generate करती हैं, और आपके view code के touch करने से पहले submitted data validate करती हैं।

Note: एक Form class को एक form की fields के लिए single source of truth समझें।

उदाहरण: Why Use Django Forms

Django Forms are Python classes that describe what fields a form has, generate the HTML automatically, and validate submitted data before your view code touches it.

markup
from django import forms


class ContactForm(forms.Form):
    name = forms.CharField(max_length=100)
    email = forms.EmailField()
    message = forms.CharField(widget=forms.Textarea)
{# 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. #}

एक View में Form उपयोग करना

एक view एक form instance बनाता है, इसे एक template को pass करता है, और बाद में validate करने के लिए submitted data के साथ इसे फिर से बनाता है।

Note:
  • एक empty ContactForm() blank fields render करता है
  • ContactForm(request.POST) submitted data से bound होता है।

उदाहरण: Using a Form in a View

A view creates a form instance, passes it to a template, and later re-creates it with submitted data to validate.

markup
from django.shortcuts import render


def contact(request):
    form = ContactForm()
    return render(request, 'contact.html', {'form': 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 को Render करना

Django automatically एक single template variable से form की हर field render कर सकता है, हर field को paragraph tags में wrap करते हुए।

उदाहरण: Rendering the Form

Django can render every field of a form automatically with a single template variable, wrapping each field in paragraph tags.

markup
<form method="post">
  {% csrf_token %}
  {{ form.as_p }}
  <button type="submit">Send</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. #}
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. Django की Form class उपयोग करने के बजाय raw HTML forms हाथ से बनाना, built-in validation और cleanup खोते हुए।
  2. यह भूल जाना कि एक Form object सिर्फ तभी data रखता है जब आप इसे request.POST से construct करें।
  3. यह मान लेना कि एक form valid है सिर्फ इसलिए कि यह page पर render हुआ, is_valid() call किए बिना।
चैप्टर सारांश
  • Django का forms app form definition (fields), rendering (HTML), और validation (cleaning) को एक reusable class में अलग करता है।
  • एक Form class forms.py में रहती है और एक model या plain input set की structure mirror करती है।
  • Forms आपको repetitive HTML और manual validation code हाथ से लिखने से बचाते हैं।

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.