Form Fields and Widgets
In this page:
Common Field Types
Django ships field classes for text, numbers, emails, booleans, and multiple-choice values, each validating its own data type.
Example: 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. #}
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.
Example: 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. #}
Customizing Widget Attributes
Pass attrs to a widget to add HTML attributes like placeholder or CSS classes.
Example: 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. #}
- Confusing a field (validation rules) with a widget (HTML rendering) — they are separate concepts.
- Using CharField for a number, which accepts any text instead of validating it's numeric.
- Forgetting to pass 'widget=forms.Textarea' when a field needs a multi-line box, leaving it as a single-line input.
- Common field types include CharField, EmailField, IntegerField, BooleanField, and ChoiceField.
- Every field has a default widget, but you can swap it — e.g. CharField with a Textarea widget.
- Widgets control HTML attributes like placeholder, rows, and CSS classes via the attrs argument.
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: