私はdjangoを初めて使用します。
私はこの投票アプリを持っていますが、投票ごとにセッションごとに1票で投票者を制限したいと思います。例:
投票#1
投票#2
投票#1に投票するとき、投票#1に投票することはできませんが、投票#2に投票することはできます。
そこで、投票IDをリストに入れて、そこにあるかどうかを確認することにしました。
poll_list = [] #declare the poll_list variable
@login_required
@never_cache
def vote(request, poll_id):
global poll_list #declare it as global
p = get_object_or_404(Poll, pk=poll_id)
try:
selected_choice = p.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
return render_to_response('polls/detail.html', {
'poll': p,
'error_message': "You didn't select a choice.",
}, context_instance=RequestContext(request))
else:
if poll_id in request.session['has_voted']: #here is the checking happens
return HttpResponse("You've already voted.")
selected_choice.votes += 1
selected_choice.save()
poll_list.append(poll_id) #i append the poll_id
request.session['has_voted'] = poll_list #pass to a session
return HttpResponseRedirect(reverse('poll_results', args=(p.id,)))
return HttpResponse("You're voting on poll %s." % poll_id)
エラーが発生しました:
KeyError at /polls/3/vote/
'has_voted'
投票ボタンをクリックすると、このエラーが表示されます
これで私を助けることができる人は誰ですか?
ありがとう、ジャスティン