From a164985c779b3e0cb7d3fb1ba2c028af4becbee5 Mon Sep 17 00:00:00 2001 From: Jarmil <jarmil.halamicek@seznam.cz> Date: Wed, 29 Jul 2020 09:57:11 +0200 Subject: [PATCH] Pridano strankovani aktualit. Solves #10 --- .gitignore | 2 ++ senat_campaign/models.py | 13 +++++++ .../senat_campaign_news_index_page.html | 34 +++++++++++++------ 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index d72a1d6d..73c3131d 100644 --- a/.gitignore +++ b/.gitignore @@ -145,3 +145,5 @@ cython_debug/ media_files/ static_files/ +.python-version +.idea/ diff --git a/senat_campaign/models.py b/senat_campaign/models.py index 91b986a9..4e849fe8 100644 --- a/senat_campaign/models.py +++ b/senat_campaign/models.py @@ -1,3 +1,4 @@ +from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator from django.db import models from django.utils.translation import gettext_lazy from wagtail.admin.edit_handlers import ( @@ -319,6 +320,18 @@ class SenatCampaignNewsIndexPage(Page, SubpageMixin, MetadataPageMixin): def get_context(self, request): context = super().get_context(request) articles = self.get_children().live().order_by("-senatcampaignnewspage__date") + paginator = Paginator(articles, 4) + + page = request.GET.get("page") + try: + articles = paginator.page(page) + except PageNotAnInteger: + # If page is not an integer, deliver first page. + articles = paginator.page(1) + except EmptyPage: + # If page is out of range (e.g. 9999), deliver last page of results. + articles = paginator.page(paginator.num_pages) + context["articles"] = articles return context diff --git a/senat_campaign/templates/senat_campaign/senat_campaign_news_index_page.html b/senat_campaign/templates/senat_campaign/senat_campaign_news_index_page.html index 391f080e..246712d1 100644 --- a/senat_campaign/templates/senat_campaign/senat_campaign_news_index_page.html +++ b/senat_campaign/templates/senat_campaign/senat_campaign_news_index_page.html @@ -34,30 +34,44 @@ </div> <!-- /row --> - {% comment %}{# TODO #} + {% if articles.paginator.num_pages > 1 %} <nav aria-label="Stránkování aktualit"> <ul class="pagination justify-content-center"> - <li class="page-item active"> - <a class="page-link" href="#">1 <span class="sr-only">(current)</span></a> - </li> - <li class="page-item"><a class="page-link" href="#">2</a></li> - <li class="page-item"><a class="page-link" href="#">3</a></li> - <li class="page-item"> - <a class="page-link" href="#">Další</a> - </li> + + {% if articles.has_previous %} + <li class="page-item"><a class="page-link" href="?page={{ articles.previous_page_number }}">Předchozí</a></li> + {% endif %} + + {% for page_num in articles.paginator.page_range %} + {% if page_num == articles.number %} + <li class="page-item active"> + <a class="page-link" href="#">{{ page_num }} <span class="sr-only">(current)</span></a> + </li> + {% else %} + <li class="page-item"><a class="page-link" href="?page={{ page_num }}">{{ page_num }}</a></li> + {% endif %} + {% endfor %} + + {% if articles.has_next %} + <li class="page-item"><a class="page-link" href="?page={{ articles.next_page_number }}">Další</a></li> + {% endif %} + </ul> </nav> - {% endcomment %} + + {% endif %} </div><!-- /column --> {% if page.root_page.facebook %} + {% if articles.number == 1 %} <div class="col-12 col-lg-4 offset-xl-1 text-center"> <h3 class="lead mb-3">Z mého facebooku</h3> <iframe src="https://www.facebook.com/plugins/page.php?href={{ page.root_page.facebook }}&tabs=timeline&width=290&height=500&small_header=true&adapt_container_width=true&hide_cover=true&show_facepile=false&appId=410144066050191" width="290" height="500" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowTransparency="true" allow="encrypted-media"></iframe> </div><!-- /column --> + {% endif %} {% endif %} </div> <!-- /row --> -- GitLab