From b332697aba9230a9f795cb000616633dc4a326b2 Mon Sep 17 00:00:00 2001 From: Oleg Lavrovsky Date: Tue, 5 Sep 2017 10:54:43 +0200 Subject: [PATCH] Pagination --- feedler/models/models.py | 18 +++++++++++++----- feedler/templates/feedler/feed_page.html | 20 ++++++++++++++++++++ publichealth/home/models/models.py | 2 +- publichealth/home/templates/news.html | 7 ++++--- publichealth/static/css/modules/_news.scss | 2 +- 5 files changed, 39 insertions(+), 10 deletions(-) diff --git a/feedler/models/models.py b/feedler/models/models.py index 83e9240..b47fc14 100644 --- a/feedler/models/models.py +++ b/feedler/models/models.py @@ -1,13 +1,13 @@ # -*- coding: utf-8 -*- from django.db import models +from django.utils import translation +from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from wagtail.wagtailcore.models import Page, Orderable from wagtail.wagtailadmin.edit_handlers import FieldPanel from wagtail.wagtailcore.fields import RichTextField -from django.utils import translation - class Stream(models.Model): title = models.CharField(max_length=255) ident = models.CharField(max_length=255) @@ -70,13 +70,21 @@ class FeedPage(Page): elif curlang in ['fr']: entries = entries.exclude(lang='de') # Order by most recent date first - entries = entries.order_by('-published') - return entries[:10] + return entries.order_by('-published') def get_context(self, request): # Update template context context = super(FeedPage, self).get_context(request) - context['feedentries'] = self.feedentries + + # Wrap with pagination + paginator = Paginator(self.feedentries, 9) + page = request.GET.get('page') + try: + feedentries = paginator.page(page) + except (PageNotAnInteger, EmptyPage): + feedentries = paginator.page(1) + + context['feedentries'] = feedentries return context class Meta: diff --git a/feedler/templates/feedler/feed_page.html b/feedler/templates/feedler/feed_page.html index 2792e81..3a6eab6 100644 --- a/feedler/templates/feedler/feed_page.html +++ b/feedler/templates/feedler/feed_page.html @@ -19,9 +19,29 @@ +
+ + +
+
    + {% if feedentries.has_previous %} +
  • +
  • + {% endif %} + {% for page_num in feedentries.paginator.page_range %} +
  • + {{ page_num }}
  • + {% endfor %} + {% if feedentries.has_next %} +
  • +
  • + {% endif %} +
+
+
{% for entry in feedentries %}
diff --git a/publichealth/home/models/models.py b/publichealth/home/models/models.py index 5354862..6fd1eb1 100644 --- a/publichealth/home/models/models.py +++ b/publichealth/home/models/models.py @@ -239,7 +239,7 @@ class HomePage(Page): entries = entries.exclude(lang='fr') elif curlang in ['fr']: entries = entries.exclude(lang='de') - return entries[:6] + return entries[:3] def get_context(self, request): # Update template context diff --git a/publichealth/home/templates/news.html b/publichealth/home/templates/news.html index 6ebb419..168393f 100644 --- a/publichealth/home/templates/news.html +++ b/publichealth/home/templates/news.html @@ -20,7 +20,7 @@ {{ entry.body|striptags|truncatewords_html:40 }} {% endif %}

- ... + 🡆
@@ -28,7 +28,7 @@ {% empty %} {% endfor %} -

Partner news

+ {% for entry in newsentries %}
@@ -45,7 +45,8 @@ {{ entry.author }}

- + +
{% empty %} diff --git a/publichealth/static/css/modules/_news.scss b/publichealth/static/css/modules/_news.scss index f823a59..0fd3bd7 100644 --- a/publichealth/static/css/modules/_news.scss +++ b/publichealth/static/css/modules/_news.scss @@ -91,7 +91,7 @@ } .panel-body { // height: 50%; - background-color: #f0f0f0; + background-color: #ffffff; border-radius: 4px; border-top: 3px solid $brand-primary; // border: 2px solid rgba(38, 67, 169, 0.8);