From 7e7dc38c6f5cee318ec5ba9cd3072b5eb8478f18 Mon Sep 17 00:00:00 2001 From: roger Date: Mon, 19 Sep 2022 17:58:03 +0800 Subject: [PATCH] add detail view and result view, vote function can be used. --- polls/templates/detail.html | 23 ++++++++++++++++++----- polls/templates/index.html | 16 +++++++++------- polls/templates/results.html | 19 +++++++++++++++++++ polls/urls.py | 1 + polls/views.py | 21 +++++++++++++++++---- 5 files changed, 64 insertions(+), 16 deletions(-) create mode 100644 polls/templates/results.html diff --git a/polls/templates/detail.html b/polls/templates/detail.html index 840a832..93208c2 100644 --- a/polls/templates/detail.html +++ b/polls/templates/detail.html @@ -6,10 +6,23 @@

{{ question.question_text }}

- +{##} + +
+ {% csrf_token %} +
+

{{ question.question_text }}

+ {% if error_message %}

{{ error_mesaage }}

{% endif %} + {% for choice in question.choice_set.all %} + +
+ {% endfor %} +
+ +
\ No newline at end of file diff --git a/polls/templates/index.html b/polls/templates/index.html index d2d2b1b..bd8e7ca 100644 --- a/polls/templates/index.html +++ b/polls/templates/index.html @@ -5,14 +5,16 @@ Title - {% if latest_question_list %} - +{% else %} +

No polls are available.

+{% endif %} \ No newline at end of file diff --git a/polls/templates/results.html b/polls/templates/results.html new file mode 100644 index 0000000..4a898cb --- /dev/null +++ b/polls/templates/results.html @@ -0,0 +1,19 @@ + + + + + Title + + +

{{ question.question_text }}

+ + + +Vote again? + + + \ No newline at end of file diff --git a/polls/urls.py b/polls/urls.py index a30e2a5..6c8eb06 100644 --- a/polls/urls.py +++ b/polls/urls.py @@ -2,6 +2,7 @@ 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 494b046..6bba248 100644 --- a/polls/views.py +++ b/polls/views.py @@ -1,9 +1,10 @@ from django.shortcuts import render from django.shortcuts import get_object_or_404 -from django.http import HttpResponse +from django.http import HttpResponse, HttpResponseRedirect from django.http import Http404 -from .models import Question +from .models import Choice, Question from django.template import loader +from django.urls import reverse # Create your views here. @@ -40,9 +41,21 @@ def detail(request, question_id): question = get_object_or_404(Question, pk=question_id) return render(request, 'detail.html', {'question': question}) + def result(request, question_id): - return HttpResponse(f"You're looking at the result of question {question_id}.") + question = get_object_or_404(Question, pk=question_id) + return render(request, 'results.html', {'question': question}) + # return HttpResponse(f"You're looking at the result of question {question_id}.") def vote(request, question_id): - return HttpResponse(f"You're voting on question {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, 'detail.html', {'question': question, 'error_message': "You didn't select a choice.", }) + else: + selected_choice.votes += 1 + selected_choice.save() + return HttpResponseRedirect(reverse('polls:result', args=(question_id,))) + # return HttpResponse(f"You're voting on question {question_id}")