Form Class बनाना
class FormName(forms.Form):
required_field = forms.CharField(max_length=100)
optional_field = forms.CharField(required=False)
email = forms.EmailField()
एक Form Class Define करना
आप हर field जो class attribute की तरह declare करते हैं वह rendered form पर एक input बन जाती है, आपके declare करने के order में।
उदाहरण: Defining a Form Class
Every field you declare as a class attribute becomes one input on the rendered form, in the order you declare them.
from django import forms
class SignupForm(forms.Form):
username = forms.CharField(max_length=50)
age = forms.IntegerField()
{# 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. #}
Required बनाम Optional Fields
Fields default रूप से required हैं; एक field को optional बनाने के लिए required=False pass करें।
उदाहरण: Required vs Optional Fields
Fields are required by default; pass required=False to make a field optional.
class SignupForm(forms.Form):
username = forms.CharField(max_length=50)
bio = forms.CharField(required=False)
{# 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. #}
एक Default Label Set करना
Django field name से automatically एक label generate करता है, लेकिन आप इसे label argument से override कर सकते हैं।
उदाहरण: Setting a Default Label
Django auto-generates a label from the field name, but you can override it with the label argument.
class SignupForm(forms.Form):
username = forms.CharField(max_length=50, label='Choose a username')
{# 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 को इससे अलग नाम देना कि view बाद में cleaned_data से उन्हें कैसे पढ़ता है।
- forms.Form को subclass करने से पहले 'from django import forms' import करना भूल जाना।
- Form classes को एक separate forms.py के बजाय views.py के अंदर रखना, app को navigate करना कठिन बनाते हुए।
- एक form class forms.Form को subclass करती है और हर field के लिए एक attribute declare करती है।
- हर field type (CharField, EmailField, IntegerField) automatically अपना validation rules enforce करता है।
- Form classes forms.py में रहती हैं, views.py में import होती हैं जहां उन्हें उपयोग किया जाता है।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: