Django Forms का परिचय
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 करती हैं।
उदाहरण: 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.
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 के साथ इसे फिर से बनाता है।
- एक 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.
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.
<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. #}
- Django की Form class उपयोग करने के बजाय raw HTML forms हाथ से बनाना, built-in validation और cleanup खोते हुए।
- यह भूल जाना कि एक Form object सिर्फ तभी data रखता है जब आप इसे request.POST से construct करें।
- यह मान लेना कि एक 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 हाथ से लिखने से बचाते हैं।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: