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'])
The CharField with TextInput widget renders an <input type="text"> element. The attrs dictionary adds HTML attributes like class and placeholder. Django also adds name, id, and required by default.
forms.EmailField is the correct field type for emails. forms.EmailInput is the correct widget for HTML5 email input. Option B uses both correctly.
Option B uses TextInput with a manual type attribute, which is not recommended. Option B mismatches field and widget types. Option B uses a non-existent widget class.
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']
Django form fields convert input strings to Python types during validation. IntegerField converts the string '25' to the integer 25 in cleaned_data. Since 25 is within the allowed range, validation passes.
from django import forms class MyForm(forms.Form): choices = [('1', 'One'), ('2', 'Two')] option = forms.ChoiceField(widget=forms.Select(choices=choices))
The choices argument belongs to the ChoiceField constructor, not the widget. Although the Select widget accepts a choices argument, Django overrides the widget's choices with the field's own choices (which are empty), resulting in an empty dropdown.
ClearableFileInput is the standard widget for file inputs in Django. Adding attrs={'multiple': True} enables multiple file selection in browsers. Option A is invalid because FileInput does not accept a direct multiple argument. Option A does not exist. Option A disables multiple selection.