Form का Validation
In this page:
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.
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.
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 }}.
<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. #}
request.POSTvalues को पहलेform.is_valid()call किए बिना directly पढ़ना, जो validation को पूरी तरह skip कर देता है।is_valid()call करने से पहलेform.cleaned_dataaccess करना, जो एक error raise करता है क्योंकि cleaning अभी run नहीं हुई।- यह मान लेना कि client-side HTML5 validation (जैसे
required) काफी है, और यह नज़रअंदाज़ करना कि server को हमेशा फिर से validate करना चाहिए।
form.is_valid()call करना हर field की validation run करता है और success परform.cleaned_datapopulate करता है।- Invalid submissions हर खराब field के लिए अपने आप
form.errorsमें error messages attach करती हैं। request.POSTसे directly आए data पर कभी भरोसा न करें — हमेशा पहले form के through validate करें।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: