How to get the value from the drop down box django?
give a name to tag, like
<select name="dropdown"> <option selected="selected" disabled>Objects on page:</option> <option value="10">10</option> <option value="20">20</option> <option value="30">30</option> <option value="40">40</option> <option value="50">50</option> </select>
Access it in view like
def PageObjects(request): q = bla_bla_bla(bla_bla) answer = request.GET['dropdown']
I would recommend sending your data with post:
<form action="PageObjects" method="post"> <select > <option selected="selected" disabled>Objects on page:</option> <option value="10">10</option> <option value="20">20</option> <option value="30">30</option> <option value="40">40</option> <option value="50">50</option> </select> <input type="submit" value="Select"></form>
And you should access your form values through the cleaned_data
dictionary:
def page_objects(request): if request.method == 'POST': form = YourForm(request.POST) if form.is_valid(): answer = form.cleaned_data['value']
I really recommend that you read the Django docs:
https://docs.djangoproject.com/en/1.4/topics/forms/#using-a-form-in-a-view
make a file forms.py inside of 'your_app_folder'
in forms.py:
class FilterForm(forms.Form): FILTER_CHOICES = ( ('time', 'Time'), ('timesince', 'Time Since'), ('timeuntil', 'Time Untill'), ) filter_by = forms.ChoiceField(choices = FILTER_CHOICES)
in views.py
from .forms import FilterFormdef 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
this form will generate the following 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>
Notice the option field with attribute selected, when you submit the form, in your views.py file you will grab the data from selected
attribute with the line
answer = form.cleaned_data.get('filter_by')