forms.py 'your_app_folder'
forms.py:
class FilterForm(forms.Form):
FILTER_CHOICES = (
('time', 'Time'),
('timesince', 'Time Since'),
('timeuntil', 'Time Untill'),
)
filter_by = forms.ChoiceField(choices = FILTER_CHOICES)
views.py
from .forms import FilterForm
def name_of_the_page(request):
form = FilterForm(request.POST or None)
answer = ''
if form.is_valid():
answer = form.cleaned_data.get('filter_by')
// notice `filter_by` matches the name of the variable we designated
// in forms.py
html:
<tr><th><label for="id_filter_by">Filter by:</label></th><td><select id="id_filter_by" name="filter_by" required>
<option value="time" selected="selected">Time</option>
<option value="timesince">Time Since</option>
<option value="timeuntil">Time Untill</option>
</select></td></tr>
Pay attention to the parameter field with the selected attribute when submitting the form to the views.py file, you will capture data from the attribute selectedusing the line
answer = form.cleaned_data.get('filter_by')
source
share