Form fields let users enter data. Widgets show how these fields look on the page.
You want to collect user input like names or emails.
You need to display a dropdown list for users to pick an option.
You want to show a checkbox for yes/no questions.
You want to customize how a form input looks or behaves.
You want to validate user input before saving it.
Syntax
Django
from django import forms
class MyForm(forms.Form):
name = forms.CharField(widget=forms.TextInput(attrs={'class': 'my-class'}))
age = forms.IntegerField(widget=forms.NumberInput())
agree = forms.BooleanField(widget=forms.CheckboxInput())
Each form field has a type like CharField or IntegerField.
Widgets control the HTML input element shown for that field.
A. <input type="text" class="email-field" aria-label="Email address" name="email" required>
B. <input type="email" class="email-field" aria-label="Email address" name="email" required>
C. <textarea class="email-field" aria-label="Email address" name="email" required></textarea>
D. <input type="email" name="email" required>
Solution
Step 1: Identify the widget type
The widget is EmailInput, which renders an input with type="email".
Step 2: Check the attributes added
The attrs dictionary adds class="email-field" and aria-label="Email address" to the input element.
Final Answer:
<input type="email" class="email-field" aria-label="Email address" name="email" required> -> Option B
Quick Check:
EmailField + EmailInput widget = input type email with attrs [OK]
Hint: EmailInput widget renders input type email with given attrs [OK]
Common Mistakes:
Confusing EmailInput with TextInput
Ignoring attrs dictionary
Expecting textarea instead of input
4. What is wrong with this Django form field declaration?
age = forms.IntegerField(widget=forms.TextInput(attrs={'type': 'number'}))
medium
A. Using TextInput widget with IntegerField is incorrect; use NumberInput instead
B. attrs dictionary cannot contain 'type' attribute
C. IntegerField does not accept widgets
D. The syntax is correct and will work as expected
Solution
Step 1: Understand widget compatibility
IntegerField expects a widget that supports numeric input, like NumberInput.
Step 2: Analyze the widget used
Using TextInput with attrs={'type': 'number'} tries to force input type but is not the recommended way and may cause issues.
Final Answer:
Using TextInput widget with IntegerField is incorrect; use NumberInput instead -> Option A
Quick Check:
IntegerField + NumberInput widget is correct combo [OK]
Hint: Use NumberInput widget for IntegerField, not TextInput [OK]
Common Mistakes:
Forcing input type in attrs instead of using correct widget
Assuming all widgets work with all fields
Ignoring widget-field compatibility
5. You want to create a Django form field for a password input that:
- Hides the typed characters
- Has a placeholder saying 'Enter your password'
- Adds a CSS class 'password-input'
Which of the following is the correct way to declare this field?
hard
A. password = forms.CharField(widget=forms.PasswordInput(placeholder='Enter your password', class='password-input'))
B. password = forms.PasswordField(widget=forms.TextInput(attrs={'placeholder': 'Enter your password', 'class': 'password-input'}))
C. password = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Enter your password', 'class': 'password-input'}))
D. password = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'Enter your password', 'class': 'password-input'}))
Solution
Step 1: Choose the correct field and widget
Password inputs use CharField with PasswordInput widget to hide characters.
Step 2: Add attributes correctly
Attributes like placeholder and class must be inside the attrs dictionary passed to the widget.
Step 3: Check each option
password = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'Enter your password', 'class': 'password-input'})) correctly uses PasswordInput(attrs={...}). password = forms.PasswordField(widget=forms.TextInput(attrs={'placeholder': 'Enter your password', 'class': 'password-input'})) uses non-existent PasswordField. password = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Enter your password', 'class': 'password-input'})) uses TextInput which renders type="text" and does not hide characters. password = forms.CharField(widget=forms.PasswordInput(placeholder='Enter your password', class='password-input')) passes attrs incorrectly.
Final Answer:
password = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'Enter your password', 'class': 'password-input'})) -> Option D
Quick Check:
PasswordInput widget + attrs dict = correct password field [OK]
Hint: Use PasswordInput widget with attrs dict for placeholders and classes [OK]
Common Mistakes:
Using non-existent PasswordField
Passing attrs outside attrs dictionary
Using TextInput instead of PasswordInput
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.