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

Form Class बनाना

एक form class एक blueprint है जो Django को exactly बताती है कि एक person को कौन से boxes भरने हैं, एक recipe card की तरह जो हर ingredient list करता है।
Syntax
markup
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 में।

Note: Class में field order वही order होता है जिसमें fields page पर render होती हैं।

उदाहरण: 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.

markup
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.

markup
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.

markup
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. #}
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. Form fields को इससे अलग नाम देना कि view बाद में cleaned_data से उन्हें कैसे पढ़ता है।
  2. forms.Form को subclass करने से पहले 'from django import forms' import करना भूल जाना।
  3. 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 होती हैं जहां उन्हें उपयोग किया जाता है।

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.