0
0
Djangoframework~20 mins

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

Choose your learning style9 modes available
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.