Should I use the next construction?
def PageObjects(request):
q = bla_bla_bla(bla_bla)
answer = request.POST['value']
<form action="PageObjects" method="get">
<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>
How can I solve this problem? What do I need to write?
Using Form in a View In Django, the request object passed as parameter to your view has an attribute called "method" where the type of the request is set, and all data passed via POST can be accessed via the request. POST dictionary. The view will display the result of the login form posted through the loggedin.
to use select = request. form. get('comp_select') in the test view to get the value of the select element with the name attribute set to comp_select . to add a form with the select element with the name attribute set to comp_select .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With