diff --git a/db.sqlite3 b/db.sqlite3 index 62dfcc8..b44bc11 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ diff --git a/polls/__pycache__/urls.cpython-310.pyc b/polls/__pycache__/urls.cpython-310.pyc index 95d9765..4aee201 100644 Binary files a/polls/__pycache__/urls.cpython-310.pyc and b/polls/__pycache__/urls.cpython-310.pyc differ diff --git a/polls/__pycache__/views.cpython-310.pyc b/polls/__pycache__/views.cpython-310.pyc index 1e838dc..3066ff2 100644 Binary files a/polls/__pycache__/views.cpython-310.pyc and b/polls/__pycache__/views.cpython-310.pyc differ diff --git a/polls/templates/polls/detail.html b/polls/templates/polls/detail.html index e38ffba..f2b84f4 100644 --- a/polls/templates/polls/detail.html +++ b/polls/templates/polls/detail.html @@ -1,6 +1,14 @@ -

{{ question.question_text }}

- \ No newline at end of file +
+ {% csrf_token %} +
+ +

{{ question.question_text }}

+
+ {% if error_message %}

{{ error_message }}

{% endif %} + {% for choice in question.choice_set.all %} + +
+ {% endfor %} +
+ +
\ No newline at end of file diff --git a/polls/templates/polls/results.html b/polls/templates/polls/results.html new file mode 100644 index 0000000..f9b3666 --- /dev/null +++ b/polls/templates/polls/results.html @@ -0,0 +1,9 @@ +

{{ question.question_text }}

+ + + +Vote again? \ No newline at end of file diff --git a/polls/urls.py b/polls/urls.py index 8836fe6..a2b6cc3 100644 --- a/polls/urls.py +++ b/polls/urls.py @@ -4,8 +4,8 @@ from . import views app_name = "polls" urlpatterns = [ - path("", views.index, name="index"), - path("/", views.detail, name="detail"), - path("/results/", views.results, name="results"), + path("", views.IndexView.as_view(), name="index"), + path("/", views.DetailView.as_view(), name="detail"), + path("/results/", views.ResultsView.as_view(), name="results"), path("/vote/", views.vote, name="vote"), ] \ No newline at end of file diff --git a/polls/views.py b/polls/views.py index 79e2140..eb84752 100644 --- a/polls/views.py +++ b/polls/views.py @@ -1,19 +1,48 @@ +from django.db.models import F +from django.http import HttpResponseRedirect from django.shortcuts import get_object_or_404, render -from django.http import HttpResponse -from .models import Question +from django.urls import reverse +from django.views import generic -def index(request): - latest_question_list = Question.objects.order_by("-pub_date")[:5] - context = {"latest_question_list": latest_question_list} - return render(request, "polls/index.html", context) +from .models import Choice, Question + + +class IndexView(generic.ListView): + template_name = "polls/index.html" + context_object_name = "latest_question_list" + + def get_queryset(self): + """Return the last five published questions.""" + return Question.objects.order_by("-pub_date")[:5] -def detail(request, question_id): - question = get_object_or_404(Question, pk=question_id) - return render(request, "polls/detail.html", {"question": question}) -def results(request, question_id): - response = "You're looking at the results of question %s." - return HttpResponse(response % question_id) +class DetailView(generic.DetailView): + model = Question + template_name = "polls/detail.html" + + +class ResultsView(generic.DetailView): + model = Question + template_name = "polls/results.html" def vote(request, question_id): - return HttpResponse("You're voting on question %s." % question_id) \ No newline at end of file + question = get_object_or_404(Question, pk=question_id) + try: + selected_choice = question.choice_set.get(pk=request.POST["choice"]) + except (KeyError, Choice.DoesNotExist): + # Redisplay the question voting form. + return render( + request, + "polls/detail.html", + { + "question": question, + "error_message": "You didn't select a choice.", + }, + ) + else: + selected_choice.votes = F("votes") + 1 + selected_choice.save() + # Always return an HttpResponseRedirect after successfully dealing + # with POST data. This prevents data from being posted twice if a + # user hits the Back button. + return HttpResponseRedirect(reverse("polls:results", args=(question.id,))) \ No newline at end of file