13π
β
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
π€Jens
23π
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']
π€Paritosh Singh
- [Django]-Problems with contenttypes when loading a fixture in Django
- [Django]-What does RunPython.noop() do?
- [Django]-Access Django models with scrapy: defining path to Django project
2π
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 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
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')
π€nodefault
- [Django]-Writing a custom management command with args and options β explanation of fields needed
- [Django]-Parentheses in django if statement
- [Django]-Manually create a Django QuerySet or rather manually add objects to a QuerySet
Source:stackexchange.com