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

Form Fields and Widgets

A field decides what kind of answer is allowed, and a widget decides what the input box looks like on screen.

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.

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. #}

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.

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. #}

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.

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. #}
Common Mistakes
  1. Confusing a field (validation rules) with a widget (HTML rendering) — they are separate concepts.
  2. Using CharField for a number, which accepts any text instead of validating it's numeric.
  3. Forgetting to pass 'widget=forms.Textarea' when a field needs a multi-line box, leaving it as a single-line input.
Chapter Summary
  • 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.

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.