2012-05-23 15 views
1

用戶經由一個複選框選擇的槽,並且還應該輸入用戶名,如示於下面的模板:如何在django中修復GET參數錯誤?

<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" /> 

我然後想顯示已鍵入,並且在選擇的時間的用戶名複選框。我這樣做是通過下面的圖和模板:

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}} 

當我去,雖然vote.html,我總是得到錯誤信息,但(請輸入用戶名並選擇時間)。在沒有選擇2個GET參數的視圖中有什麼不正確?

回答

2

您正在使用您的形式POST要求:

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

但在視圖中,要檢查的GET對象,來自GET要求:

request.GET 

改變你的形式方法method="get"解決問題。

編輯:瞭解更多關於GET VS POST要求在這裏:When do you use POST and when do you use GET?

+0

doh!感謝您的幫助 – sharataka

+0

@sharataka很高興幫助:) –