From 477903dfd353dacc03c2d12a9c6d985e8154e3cb Mon Sep 17 00:00:00 2001 From: sosokker Date: Mon, 28 Aug 2023 22:32:42 +0700 Subject: [PATCH] Update Views of all pages - index, detail, result / Add Home Page Update views of all pages and add HomeView (Homepage) + Update data of sqlite3 polls database --- db.sqlite3 | Bin 143360 -> 143360 bytes mysite/settings.py | 3 +- mysite/urls.py | 3 + polls/static/polls/base.css | 191 +++++++++++++++++++++++++++++ polls/static/polls/style.css | 6 - polls/templates/polls/base.html | 25 ++++ polls/templates/polls/detail.html | 36 ++++-- polls/templates/polls/home.html | 26 ++++ polls/templates/polls/index.html | 29 +++-- polls/templates/polls/result.html | 9 -- polls/templates/polls/results.html | 24 ++-- polls/views.py | 47 ++----- 12 files changed, 312 insertions(+), 87 deletions(-) create mode 100644 polls/static/polls/base.css create mode 100644 polls/templates/polls/base.html create mode 100644 polls/templates/polls/home.html delete mode 100644 polls/templates/polls/result.html diff --git a/db.sqlite3 b/db.sqlite3 index 8d7401227eca2b25d12df0886f67e2e2c8d7aa31..4249e6f2af849308e410f8ef7ae9ae4d5c62ceb7 100644 GIT binary patch delta 161 zcmZp8z|ru4V}dlJ(?l6(MyJMv)&$0_2~3Ol`Cc%v@b6{dzrlZoe=q;n&4LAM`Ga}c zSQ(@lIRh$7GV=2jnYdV48H5=bUDMNm7+urTnRr=P8KgNGoij3%vs3e!7#J8>r8(F^(wxrP+B|F^S + + MySite Polls + + + + +
+
+ + +
+ {% block content %} + {% endblock content %} +
+ + diff --git a/polls/templates/polls/detail.html b/polls/templates/polls/detail.html index 6df8776..8ca20b6 100644 --- a/polls/templates/polls/detail.html +++ b/polls/templates/polls/detail.html @@ -1,12 +1,24 @@ -
- {% 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 +{% extends 'polls/base.html' %} + +{% block content %} +
+
+
+ {% csrf_token %} +
+ {{ question.question_text }} + {% if error_message %} +

{{ error_message }}

+ {% endif %} + {% for choice in question.choice_set.all %} +
+ + +
+ {% endfor %} +
+ +
+
+
+{% endblock content %} diff --git a/polls/templates/polls/home.html b/polls/templates/polls/home.html new file mode 100644 index 0000000..ad8a1c8 --- /dev/null +++ b/polls/templates/polls/home.html @@ -0,0 +1,26 @@ +{% extends 'polls/base.html' %} + +{% block content %} +
+
+
+

Welcome to KU Polls

+

Explore and participate in our weird poll questions.

+
+
+
+

Recent Polls

+

Total number of polls: {{ total_polls }}

+
+ {% if latest_question_list %} + {% for question in latest_question_list %} + + {% endfor %} + {% else %} +

No polls are available.

+ {% endif %} +
+
+{% endblock content %} diff --git a/polls/templates/polls/index.html b/polls/templates/polls/index.html index 04410ae..ee0d853 100644 --- a/polls/templates/polls/index.html +++ b/polls/templates/polls/index.html @@ -1,13 +1,18 @@ -{% load static %} +{% extends 'polls/base.html' %} - - -{% if latest_question_list %} - -{% else %} -

No polls are available.

-{% endif %} \ No newline at end of file +{% block content %} +
+

Recent Polls

+
+ {% if latest_question_list %} + {% for question in latest_question_list %} + + {% endfor %} + {% else %} +

No polls are available.

+ {% endif %} +
+
+{% endblock content %} diff --git a/polls/templates/polls/result.html b/polls/templates/polls/result.html deleted file mode 100644 index 33b67b2..0000000 --- a/polls/templates/polls/result.html +++ /dev/null @@ -1,9 +0,0 @@ -

{{ 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/templates/polls/results.html b/polls/templates/polls/results.html index 33b67b2..dd55c8d 100644 --- a/polls/templates/polls/results.html +++ b/polls/templates/polls/results.html @@ -1,9 +1,17 @@ -

{{ question.question_text }}

+{% extends 'polls/base.html' %} -
    -{% 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 +{% block content %} +
+
+
+

{{ question.question_text }}

+
+
    + {% for choice in question.choice_set.all %} +
  • {{ choice.choice_text }} — {{ choice.votes }} vote{{ choice.votes|pluralize }}
  • + {% endfor %} +
+ Vote again? +
+
+{% endblock content %} diff --git a/polls/views.py b/polls/views.py index 57448fd..320229c 100644 --- a/polls/views.py +++ b/polls/views.py @@ -3,50 +3,19 @@ from django.shortcuts import get_object_or_404, render from django.urls import reverse from django.views import generic from django.utils import timezone +from django.views.generic import TemplateView from .models import Choice, Question -# 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) +class HomeView(TemplateView): + template_name = 'polls/home.html' - -# def index(request): -# lastest_question_list = Question.objects.order_by("-pub_date")[:5] -# template = loader.get_template("polls/index.html") -# context = { -# "lastest_question_list": lastest_question_list -# } -# return HttpResponse(template.render(context, request)) - - -# def detail(request, question_id): -# try: -# question = Question.objects.get(pk=question_id) -# except Question.DoesNotExist: -# raise Http404("Question does not exist") -# return render(request, "polls/detail.html", {"question": question}) - - -# 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) - - -# def results(request, question_id): -# question = get_object_or_404(Question, pk=question_id) -# return render(request, "polls/results.html", {"question": question}) - - -# def vote(request, question_id): -# return HttpResponse("You're voting on question %s." % question_id) + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context['latest_question_list'] = Question.objects.filter(pub_date__lte=timezone.now()).order_by("-pub_date")[:5] + context['total_polls'] = Question.objects.count() + return context class IndexView(generic.ListView):