1

The user selects a slot via a checkbox and also should enter a username, as shown in the template below:

<form action="/clubs/{{ club.id }}/vote/" method="post">
{% csrf_token %}
{% for slot in tom_open_slots %}
    <input type="checkbox" name="slot" id="slot{{ forloop.counter }}" value="{{ slot.id }}" />
    <label for="slot{{ forloop.counter }}">{{ slot.slot }} on Court {{slot.court}}</label><br />
{% endfor %}    
<input type="text" name="username" />
<input type="submit" value="Reserve" />

I then would like to display the username that was typed and time selected in the checkbox. I do this through the view and template below:

def vote(request, club_id):
    if 'username' in request.GET and request.GET['username'] and 'slot' in request.GET and request.GET['slot']:
        username = request.GET['username']
        slot = request.GET['slot']
        return render_to_response('reserve/templates/vote.html',{'username':username, 'slot':slot})
    else:
        return HttpResponse('Please enter a username and select a time.')


{{slot}}
{{username}}

When I go to vote.html though, I always get the error message though (Please enter a username and select a time). What is incorrect in the view that is not picking up the 2 GET parameters?

sharataka
  • 5,014
  • 20
  • 65
  • 125

2 Answers2

2

You are using POST request in your form:

<form action="/clubs/{{ club.id }}/vote/" method="post">

But in the view, you are checking the GET object which comes from GET request:

request.GET

Change your form method to method="get" to fix the problem.

Edit: read more on GET vs POST request here: When do you use POST and when do you use GET?

Community
  • 1
  • 1
K Z
  • 29,661
  • 8
  • 73
  • 78
1

In Django, the HttpRequest object has three dictionaries which give you the request parameters:

  • request.GET gives you the query string parameters,

  • request.POST gives you the post data, and

  • request.REQUEST gives you both.

In your case, since the form is using the POST method, you should use either request.POST or request.REQUEST.

FYI: https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.GET

Isaac Sutherland
  • 3,082
  • 4
  • 28
  • 37