Form Fields और Widgets
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.
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.
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.
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. #}
- एक field (validation rules) को एक widget (HTML rendering) के साथ confuse करना — ये separate concepts हैं।
- एक number के लिए CharField उपयोग करना, जो यह validate करने के बजाय कि यह numeric है किसी भी text को accept करता है।
- जब एक 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 करते हैं।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: