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

Form Fields और Widgets

एक field decide करती है कि किस तरह का answer allowed है, और एक widget decide करता है कि screen पर input box कैसा दिखता है।
Syntax
markup
class FormName(forms.Form):
    field_name = forms.FieldType(widget=forms.WidgetType(attrs={'class': 'value'}))

# Example: forms.CharField(widget=forms.Textarea)

Common Field Types

Django text, numbers, emails, booleans, और multiple-choice values के लिए field classes ship करता है, हर एक अपना data type validate करते हुए।

उदाहरण: Common Field Types

Django ships field classes for text, numbers, emails, booleans, and multiple-choice values, each validating its own data type.

markup
class ProfileForm(forms.Form):
    email = forms.EmailField()
    age = forms.IntegerField()
    newsletter = forms.BooleanField(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. #}

एक Widget चुनना

एक widget सिर्फ HTML rendering बदलता है, validation नहीं — एक CharField एक text input के बजाय एक Textarea के रूप में render हो सकता है।

उदाहरण: Choosing a Widget

A widget changes only the HTML rendering, not the validation — a CharField can render as a Textarea instead of a text input.

markup
class ProfileForm(forms.Form):
    bio = 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. #}

Widget Attributes को Customize करना

placeholder या CSS classes जैसे HTML attributes जोड़ने के लिए एक widget को attrs pass करें।

उदाहरण: Customizing Widget Attributes

Pass attrs to a widget to add HTML attributes like placeholder or CSS classes.

markup
class ProfileForm(forms.Form):
    email = forms.EmailField(
        widget=forms.EmailInput(attrs={'placeholder': '[email protected]'})
    )
{# 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. एक field (validation rules) को एक widget (HTML rendering) के साथ confuse करना — ये separate concepts हैं।
  2. एक number के लिए CharField उपयोग करना, जो यह validate करने के बजाय कि यह numeric है किसी भी text को accept करता है।
  3. जब एक field को multi-line box चाहिए तो 'widget=forms.Textarea' pass करना भूल जाना, इसे एक single-line input छोड़ते हुए।
चैप्टर सारांश
  • Common field types में CharField, EmailField, IntegerField, BooleanField, और ChoiceField शामिल हैं।
  • हर field का एक default widget होता है, लेकिन आप इसे swap कर सकते हैं — जैसे CharField एक Textarea widget के साथ।
  • Widgets attrs argument के through placeholder, rows, और CSS classes जैसे HTML attributes control करते हैं।

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.