From e04367b4f1067967cb283a8309e48c66a3597be4 Mon Sep 17 00:00:00 2001 From: sascha Date: Wed, 10 Apr 2024 09:07:21 +0000 Subject: [PATCH] django tutorial part 4 --- db.sqlite3 | Bin 143360 -> 143360 bytes polls/__pycache__/urls.cpython-310.pyc | Bin 446 -> 470 bytes polls/__pycache__/views.cpython-310.pyc | Bin 1058 -> 1747 bytes polls/templates/polls/detail.html | 20 ++++++--- polls/templates/polls/results.html | 9 ++++ polls/urls.py | 6 +-- polls/views.py | 55 ++++++++++++++++++------ 7 files changed, 68 insertions(+), 22 deletions(-) create mode 100644 polls/templates/polls/results.html diff --git a/db.sqlite3 b/db.sqlite3 index 62dfcc8034af37b745344d3ba9e12ef2846723ee..b44bc11ef51233972c2ef10542d805e986ac4497 100644 GIT binary patch delta 293 zcmZp8z|ru4V}dlJ?nD`9M%|4G5&1%f1_}mdRz@aPrUrT@CPwC_mYYxITPbib@$O*Y zyTiLj%Y zH+u&8n_0O8x#kx~xfyxs>IV7yItS+a7G)U~6$TmR=7h)R15h+~V}iOG&K=%S@~ delta 179 zcmcb{ypNeTpO=@50SNv^i>5hE_8V3ae@etVI&Q7@fHInD`h$kmC;kUED1e diff --git a/polls/__pycache__/views.cpython-310.pyc b/polls/__pycache__/views.cpython-310.pyc index 1e838dccea427934b1cdad22cda0a54367ffe2cc..3066ff20c572690119e59acb0f22a0d8ab96cdfa 100644 GIT binary patch literal 1747 zcmZ`(&5s*36t_JenIx0lY<9bCDS`-rsvJmq=m{aTQd>R*3b2Ku&Sf--y~%|62)47` zjndPWV=tUJw8#ESzH-`gIaQ+KJx{Vk0zC4Q{rtx7z2Eyim#tQV!1(jW!;{b_xSIgKdAHOpA;M2=lMnVWl&m-~@#_ik3pgD9|TFRSN`r~zxA*Rp0F zMq%EHT9kZ2c);t=2(OFAZ0Ev>c0tqNP0%z!(*{i&G$C(+rnT1W@g2VVj6@yYen@(I zmk<}}QA0lmaqy+q^JAgrWue5e;Hea2-D75NB6M7iX0VG(8Q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`VK5E221~dZZbl1ybDD4 zkRjG}7j}O^Vg-IfqFW1?*8v}{08f=gK2`k(i2VV&i4np*fi*N32?*|i7{{ question.question_text }} -
    - {% for choice in question.choice_set.all %} -
  • {{ choice.choice_text }}
  • - {% endfor %} -
\ 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 }}

+ +
    + {% for choice in question.choice_set.all %} +
  • {{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}
  • + {% endfor %} +
+ +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