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

Form का Validation

Validation Django का हर answer को form पर trust करने और save करने से पहले double-check करना है।
Syntax
markup
form = FormName(request.POST)
if form.is_valid():
    value = form.cleaned_data['field_name']
else:
    errors = form.errors

Validity जांचना

is_valid() सभी field validators run करता है और सिर्फ तब True return करता है जब हर field pass हो।

उदाहरण: Checking Validity

is_valid() runs all field validators and returns True only if every field passes.

markup
form = ContactForm(request.POST)
if form.is_valid():
    print(form.cleaned_data['email'])
{# 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. #}

Cleaned Data पढ़ना

cleaned_data validated, type-converted values का एक dictionary है, जो सिर्फ is_valid() succeed होने के बाद available है।

उदाहरण: Reading Cleaned Data

cleaned_data is a dictionary of validated, type-converted values, only available after is_valid() succeeds.

markup
form = ContactForm(request.POST)
if form.is_valid():
    name = form.cleaned_data['name']
    email = form.cleaned_data['email']
{# 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. #}

Errors Display करना

जब validation fail हो, form.errors में field names से error messages की एक dictionary होती है, जो {{ form.as_p }} से अपने आप show होती है।

उदाहरण: Displaying Errors

When validation fails, form.errors holds a dictionary of field names to error messages, shown automatically by {{ form.as_p }}.

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. request.POST values को पहले form.is_valid() call किए बिना directly पढ़ना, जो validation को पूरी तरह skip कर देता है।
  2. is_valid() call करने से पहले form.cleaned_data access करना, जो एक error raise करता है क्योंकि cleaning अभी run नहीं हुई।
  3. यह मान लेना कि client-side HTML5 validation (जैसे required) काफी है, और यह नज़रअंदाज़ करना कि server को हमेशा फिर से validate करना चाहिए।
चैप्टर सारांश
  • form.is_valid() call करना हर field की validation run करता है और success पर form.cleaned_data populate करता है।
  • Invalid submissions हर खराब field के लिए अपने आप form.errors में error messages attach करती हैं।
  • request.POST से directly आए data पर कभी भरोसा न करें — हमेशा पहले form के through validate करें।

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.