Bird
Raised Fist0
Djangoframework~20 mins

Form fields and widgets in Django - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
Form Fields & Widgets Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the rendered HTML output of this Django form field?
Consider the following Django form field definition. What HTML does it render in the browser?
Django
from django import forms

class SampleForm(forms.Form):
    name = forms.CharField(widget=forms.TextInput(attrs={'class': 'input-name', 'placeholder': 'Enter your name'}))

form = SampleForm()
print(form['name'])
A<input type="text" name="name" class="input-name" placeholder="Enter your name" required id="id_name">
B<input type="text" name="name" placeholder="Enter your name">
C<input type="text" class="input-name" id="name">
D<textarea name="name" class="input-name" placeholder="Enter your name"></textarea>
Attempts:
2 left
💡 Hint
Look at the widget type and the attributes passed in attrs.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a Django form field with a custom widget?
You want to create a Django form field for an email address that uses an HTML5 email input widget. Which code snippet is correct?
Aemail = forms.CharField(widget=forms.EmailInput(attrs={'placeholder': 'Your email'}))
Bemail = forms.EmailField(widget=forms.EmailInput(attrs={'placeholder': 'Your email'}))
Cemail = forms.EmailField(widget=forms.TextInput(attrs={'type': 'email', 'placeholder': 'Your email'}))
Demail = forms.EmailField(widget=forms.InputEmail(attrs={'placeholder': 'Your email'}))
Attempts:
2 left
💡 Hint
Check the widget class names and field types carefully.
state_output
advanced
2:00remaining
What is the cleaned_data value after form submission?
Given this Django form and submitted data, what is the value of cleaned_data['age'] after calling form.is_valid()?
Django
from django import forms

class AgeForm(forms.Form):
    age = forms.IntegerField(min_value=18, max_value=99)

submitted_data = {'age': '25'}
form = AgeForm(submitted_data)
form.is_valid()
age_value = form.cleaned_data['age']
ARaises a ValidationError
B'25' (as a string)
C25 (as an integer)
DNone
Attempts:
2 left
💡 Hint
Remember how Django form fields convert input types.
🔧 Debug
advanced
2:00remaining
Why is this Django form field definition incorrect?
Examine the following form field definition. Why does the rendered select not show the expected choices?
Django
from django import forms

class MyForm(forms.Form):
    choices = [('1', 'One'), ('2', 'Two')]
    option = forms.ChoiceField(widget=forms.Select(choices=choices))
ABecause ChoiceField requires a default value
BBecause Select widget does not accept choices argument
CBecause choices must be a dictionary, not a list of tuples
DBecause choices should be passed to ChoiceField, not to the widget
Attempts:
2 left
💡 Hint
Check where choices are expected in Django forms.
🧠 Conceptual
expert
3:00remaining
Which widget correctly supports multiple file uploads in Django forms?
You want users to upload multiple files at once using a Django form. Which widget should you use to allow selecting multiple files?
Aforms.ClearableFileInput(attrs={'multiple': True})
Bforms.FileInput(multiple=True)
Cforms.MultipleFileInput()
Dforms.FileField(widget=forms.FileInput(attrs={'multiple': False}))
Attempts:
2 left
💡 Hint
Look for the widget that supports the 'multiple' attribute properly.

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

  1. Step 1: Understand the role of form fields

    Form fields specify what kind of data the form expects, like text, numbers, or dates.
  2. Step 2: Differentiate from widgets and templates

    Widgets control how the input looks, and templates handle HTML structure, not data type.
  3. Final Answer:

    To define the type of data you want to collect from the user -> Option A
  4. 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

  1. Step 1: Recall widget attribute usage

    In Django, to add HTML attributes like placeholder, you pass them inside the widget's attrs dictionary.
  2. Step 2: Check each option's syntax

    forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Enter name'})) correctly uses widget=forms.TextInput(attrs={'placeholder': 'Enter name'}). Others misuse or omit attrs.
  3. Final Answer:

    forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Enter name'})) -> Option C
  4. Quick Check:

    Use attrs dict inside widget for HTML attributes [OK]
Hint: Use attrs dict inside widget to add HTML attributes [OK]
Common Mistakes:
  • Passing placeholder directly to CharField
  • Omitting attrs dictionary
  • Using wrong widget syntax
3. Given the form field declaration below, what HTML input element will be rendered?
email = forms.EmailField(widget=forms.EmailInput(attrs={'class': 'email-field', 'aria-label': 'Email address'}))
medium
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

  1. Step 1: Identify the widget type

    The widget is EmailInput, which renders an input with type="email".
  2. Step 2: Check the attributes added

    The attrs dictionary adds class="email-field" and aria-label="Email address" to the input element.
  3. Final Answer:

    <input type="email" class="email-field" aria-label="Email address" name="email" required> -> Option B
  4. 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

  1. Step 1: Understand widget compatibility

    IntegerField expects a widget that supports numeric input, like NumberInput.
  2. 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.
  3. Final Answer:

    Using TextInput widget with IntegerField is incorrect; use NumberInput instead -> Option A
  4. 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

  1. Step 1: Choose the correct field and widget

    Password inputs use CharField with PasswordInput widget to hide characters.
  2. Step 2: Add attributes correctly

    Attributes like placeholder and class must be inside the attrs dictionary passed to the widget.
  3. 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.
  4. Final Answer:

    password = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'Enter your password', 'class': 'password-input'})) -> Option D
  5. 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