From c8b64c8069b6bd7ba6aa42bc73aca9ec82a19f13 Mon Sep 17 00:00:00 2001 From: Jonathan DeMasi Date: Fri, 6 Mar 2020 00:40:46 -0700 Subject: pagination working --- blog/models.py | 14 ++++++++++++++ jthanio/settings/base.py | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/blog/models.py b/blog/models.py index 5ce14e4..d53fb64 100644 --- a/blog/models.py +++ b/blog/models.py @@ -1,5 +1,6 @@ from django.db import models from django import forms +from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator from wagtail.core.models import Page, Orderable from wagtail.core.fields import RichTextField, StreamField from wagtail.admin.edit_handlers import FieldPanel, InlinePanel, MultiFieldPanel, StreamFieldPanel @@ -24,6 +25,19 @@ class BlogIndexPage(Page): # Update context to include only published posts, ordered by reverse-chron context = super().get_context(request) blogpages = self.get_children().live().order_by('-first_published_at') + paginator = Paginator(blogpages, 5) + # Try to get the ?page=x value + page = request.GET.get("page") + try: + # If the page exists and the ?page=x is an int + blogpages = paginator.page(page) + except PageNotAnInteger: + # If the ?page=x is not an int; show the first page + blogpages = paginator.page(1) + except EmptyPage: + # If the ?page=x is out of range (too high most likely) + # Then return the last page + blogpages = paginator.page(paginator.num_pages) context['blogpages'] = blogpages return context diff --git a/jthanio/settings/base.py b/jthanio/settings/base.py index 4a89fac..466fd1d 100644 --- a/jthanio/settings/base.py +++ b/jthanio/settings/base.py @@ -162,4 +162,4 @@ WAGTAIL_SITE_NAME = "jthanio" # Base URL to use when referring to full URLs within the Wagtail admin backend - # e.g. in notification emails. Don't include '/admin' or a trailing slash BASE_URL = 'http://example.com' -WAGTAIL_CODE_BLOCK_THEME = 'twilight' +WAGTAIL_CODE_BLOCK_THEME = 'okaidia' -- cgit v1.2.3