Compare commits

..

2 Commits

Author SHA1 Message Date
5e60280e5e Only allow one vote per client session 2024-05-25 22:45:49 -04:00
474b551e13 Fix Log In button 2024-05-25 22:21:17 -04:00
2 changed files with 13 additions and 3 deletions

View File

@@ -83,7 +83,7 @@
{% if user.is_authenticated %} {% if user.is_authenticated %}
<a href="/logout"><button class="btn btn-outline-success">Log out</button></a> <a href="/logout"><button class="btn btn-outline-success">Log out</button></a>
{% else %} {% else %}
<button class="btn btn-primary">Log In</button> <a href="/login"><button class="btn btn-primary">Log In</button></a>
{% endif %} {% endif %}
</div> </div>
</div> </div>

View File

@@ -1,4 +1,4 @@
from django.shortcuts import render, get_object_or_404 from django.shortcuts import render, get_object_or_404, redirect
from django.http import HttpResponse, HttpResponseRedirect from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse from django.urls import reverse
from django.db.models import F from django.db.models import F
@@ -13,6 +13,8 @@ def index(request):
def detail(request, question_id): def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id) question = get_object_or_404(Question, pk=question_id)
if str(question_id) in request.session.get('votes', {}):
return redirect(f'/polls/{question_id}/results')
return render(request, "polls/detail.html", {"question": question}) return render(request, "polls/detail.html", {"question": question})
@@ -30,6 +32,10 @@ def results(request, question_id):
def vote(request, question_id): def vote(request, question_id):
question = get_object_or_404(Question, pk=question_id) question = get_object_or_404(Question, pk=question_id)
votes = request.session.get('votes', {})
if str(question_id) in votes:
print("yes")
return redirect(f'/polls/{question_id}/results')
try: try:
selected_choice = question.choice_set.get(pk=request.POST["choice"]) selected_choice = question.choice_set.get(pk=request.POST["choice"])
except (KeyError, Choice.DoesNotExist): except (KeyError, Choice.DoesNotExist):
@@ -38,4 +44,8 @@ def vote(request, question_id):
else: else:
selected_choice.votes = F("votes") + 1 selected_choice.votes = F("votes") + 1
selected_choice.save() selected_choice.save()
return HttpResponseRedirect(reverse("polls:results", args=(question.id,)))
votes[question_id] = selected_choice.id
request.session['votes'] = votes
request.session.modified = True
return HttpResponseRedirect(reverse("polls:results", args=(question.id,)))