22

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?

Venu Saini
  • 427
  • 2
  • 6
  • 19
Max L
  • 1,453
  • 2
  • 17
  • 32

3 Answers3

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'] 
Damoiskii
  • 1,328
  • 1
  • 5
  • 20
Paritosh Singh
  • 6,288
  • 5
  • 37
  • 56
  • 1
    and can i use the GET method? explain me how it will send the value from the dropbox to my view.py? – Max L Jul 20 '12 at 20:26
  • it depends on the form method you are using, if you are using
    you can use GET else use POST. In case you want it to access any (whether GET or POST) use REQUEST.
    – Paritosh Singh Jul 20 '12 at 20:29
  • In your case, you should use GET, see changes in answer – Paritosh Singh Jul 20 '12 at 20:32
  • what problem your are facing. means are you getting ?dropdown= in url... – Paritosh Singh Jul 23 '12 at 06:18
  • it's doing the action "PageObjects" and changing my URL to .../list/PageObject and show the 404 error because that URL is not written into my urls.py – Max L Jul 23 '12 at 18:23
  • then how ur form will get submit, u need to write a view for submitting your form, and what action you are writting in your form the view and url for that view should exist. If you can put here proper code snippet form and view, I can explain you better. – Paritosh Singh Jul 23 '12 at 18:35
  • the view which you wrote in question, from which url is it resolved. – Paritosh Singh Jul 23 '12 at 18:36
  • how i can do it without urls.py? – Max L Jul 23 '12 at 21:51
  • i am asking you to write regex for urls on which form is acting upon in urls.py or just write your urls.py code here. – Paritosh Singh Jul 24 '12 at 03:07
  • `from django.conf.urls.defaults import patterns from django.contrib.auth.decorators import login_required from django.views.generic.list_detail import object_detail, object_list from piston.resource import Resource from models import DevModel, RackBox, Device from forms import DevModelForm, RackBoxForm, DeviceForm from netdev.views import PageView urlpatterns = patterns('netdev.views', #RackBox (r'^rackboxes/$', login_required(PageView.as_view())), (r'^rackbox/(?P\d+)/$', login_required(object_detail), { 'queryset': RackBox.objects.all() }))` – Max L Jul 24 '12 at 10:00
  • Hi @yakudza_m, in your form you are using action="PageObjects" when you click on submit it forms url as PREVIOUS_URL+'PageObjects' and here you have got no regex for 'PageObjects' so it is not working and givng you 404 error. just give write a regex for that thing, which resolves to view PageObjects. I think you are directly writing action as name of your view which is not at all correct. – Paritosh Singh Jul 24 '12 at 10:22
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
  • 20,533
  • 11
  • 60
  • 86
  • i answered to user Paritosh Singh about my problem. how can i fix it if i will use your recommendation? – Max L Jul 23 '12 at 18:30
  • @MaxL You really should follow good http practices when deciding whether to use `POST` or `GET`. Here's a good article on how to decide which method fits your scenario http://stackoverflow.com/questions/46585/when-do-you-use-post-and-when-do-you-use-get. But I definitely don't think there's enough info here to say one over the other. – Austin A Apr 07 '15 at 03:56
  • 1
    @AustinA thanks for advice, but I asked this question 2 years ago :) Now I have more experience :) – Max L Apr 08 '15 at 06:15
  • 1
    What if you have multiple dropdowns in a form, we can declare id for each select tag, how can we access the value of a particular dropdown in the view using 'id' attribute? – Noopur Phalak Jul 10 '15 at 06:07
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
  • 372
  • 3
  • 7