diff --git a/db.sqlite3 b/db.sqlite3 index 5a7fb66..2170b7e 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ diff --git a/polls/models.py b/polls/models.py index f96326e..3b791e6 100644 --- a/polls/models.py +++ b/polls/models.py @@ -19,4 +19,4 @@ class Choice(models.Model): votes = models.IntegerField(default=0) def __str__(self): - return self.choice_text \ No newline at end of file + return self.choice_text diff --git a/polls/templates/polls/details.html b/polls/templates/polls/details.html index 54a72f3..0566bce 100644 --- a/polls/templates/polls/details.html +++ b/polls/templates/polls/details.html @@ -1,9 +1,12 @@ -

Question:

-

{{ question.question_text }}

- -

Available Choices:

- \ No newline at end of file + + + \ No newline at end of file diff --git a/polls/urls.py b/polls/urls.py index 7271bc0..a31ddea 100644 --- a/polls/urls.py +++ b/polls/urls.py @@ -2,6 +2,8 @@ from django.urls import path from . import views +app_name = "polls" + urlpatterns = [ path("", views.index, name = "index"), path("", views.detail, name="detail"), diff --git a/polls/views.py b/polls/views.py index 3175a73..c981dc1 100644 --- a/polls/views.py +++ b/polls/views.py @@ -1,6 +1,7 @@ -from django.http import HttpResponse, Http404 +from django.http import HttpResponse, Http404, HttpResponseRedirect from django.template import loader -from django.shortcuts import render +from django.shortcuts import render, get_object_or_404 +from django.urls import reverse from .models import Question, Choice @@ -24,4 +25,16 @@ def results(request, question_id): return HttpResponse(response % question_id) def vote(request, question_id): - return HttpResponse("you are voting on question %s" % question_id) + question = get_object_or_404(Question, pk=question_id) + try: + selected_choice = question.choice_set.get(pk=request.POST["choice"]) + except (KeyError, Choice.DoesNotExist): + return render(request, "polls/details.html", { + "question": question, + "error_message": "You didn't select a choice!" + }) + else: + selected_choice.votes += 1 + selected_choice.save() + + return HttpResponseRedirect(reverse("polls:results", args=(question.id,))) \ No newline at end of file