Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a form field in Django?
A form field in Django represents a single piece of data that a user can enter or select in a form. It defines the type of data expected, like text, number, or date.
Click to reveal answer
beginner
What is a widget in Django forms?
A widget controls how a form field is displayed in HTML. It decides the HTML input element used, like a text box, checkbox, or dropdown.
Click to reveal answer
intermediate
How do you change the widget of a form field in Django?
You can change a field's widget by setting the 'widget' attribute when defining the field, for example: forms.CharField(widget=forms.Textarea()) to use a textarea instead of a text input.
Click to reveal answer
beginner
Name three common Django form fields and their default widgets.
Why is it important to use widgets in Django forms?
Widgets help control the user experience by choosing the right HTML input type. They also help with accessibility and styling, making forms easier and clearer to use.
Click to reveal answer
Which Django form field would you use to accept a user's email address?
Aforms.EmailField
Bforms.CharField
Cforms.BooleanField
Dforms.DateField
✗ Incorrect
forms.EmailField is designed to accept and validate email addresses.
What widget does Django use by default for a BooleanField?
ATextInput
BRadioSelect
CSelect
DCheckboxInput
✗ Incorrect
BooleanField uses CheckboxInput by default to show a checkbox.
How can you make a CharField display as a multi-line text box?
ASet max_length to a large number
BSet widget=forms.CheckboxInput
CSet widget=forms.Textarea
DUse forms.EmailField instead
✗ Incorrect
Using widget=forms.Textarea changes the input to a multi-line text box.
Which widget would you use to let users pick from a list of options?
ACheckboxInput
BSelect
CTextInput
DTextarea
✗ Incorrect
Select widget shows a dropdown list for choosing options.
What is the main role of a widget in Django forms?
AControl how the form field is displayed in HTML
BStore form data in the database
CValidate user input
DSend form data via email
✗ Incorrect
Widgets control the HTML representation of form fields.
Explain what form fields and widgets are in Django and how they work together.
Think about the data you want and how it looks on the page.
You got /4 concepts.
Describe how you would customize a Django form field to use a different widget and why you might do that.
Consider changing a text box to a bigger input area.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of a Form field in Django?
easy
A. To define the type of data you want to collect from the user
B. To style the form with CSS classes
C. To handle database queries automatically
D. To create HTML templates for the form
Solution
Step 1: Understand the role of form fields
Form fields specify what kind of data the form expects, like text, numbers, or dates.
Step 2: Differentiate from widgets and templates
Widgets control how the input looks, and templates handle HTML structure, not data type.
Final Answer:
To define the type of data you want to collect from the user -> Option A
Quick Check:
Form field = data type definition [OK]
Hint: Form fields = data type; widgets = appearance [OK]
Common Mistakes:
Confusing widgets with form fields
Thinking form fields handle styling
Assuming form fields manage database queries
2. Which of the following is the correct way to add a placeholder attribute to a Django form field using a widget?
easy
A. forms.CharField(widget=forms.TextInput(placeholder='Enter name'))
B. forms.CharField(placeholder='Enter name')
C. forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Enter name'}))
D. forms.CharField(attrs={'placeholder': 'Enter name'})
Solution
Step 1: Recall widget attribute usage
In Django, to add HTML attributes like placeholder, you pass them inside the widget's attrs dictionary.
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]