diff --git a/.gitignore b/.gitignore index d72a1d6dae2639b21f9ad37913a78ea244accfea..73c3131d6bd6ac5908578460143d568436d2e711 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 91b986a9aa0390b2261f8a7d8075b47dd73885d7..4e849fe8f7eaedbcbda601846dabb7f01324feaf 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 391f080e6ae6d2f0e9abdece3af1056a02886a16..246712d12261acb884240857b3e4dc9614c9c217 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 -->