From 64134fdf9d847485dc1a47287666f3cb2a043bf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arnar=20Tumi=20=C3=9Eorsteinsson?= Date: Fri, 10 Feb 2017 15:28:06 +0000 Subject: [PATCH] gallery and pagination --- .../migrations/0004_auto_20170210_1445.py | 19 ++++++++++ bakerydemo/base/templatetags/gallery_tags.py | 17 +++++++++ .../base/templatetags/navigation_tags.py | 4 +- bakerydemo/breads/models.py | 25 +++++++++++-- bakerydemo/templates/base/gallery_page.html | 13 +++++++ .../templates/breads/breads_index_page.html | 37 +++++++++++++++++-- bakerydemo/templates/tags/gallery.html | 7 ++++ 7 files changed, 115 insertions(+), 7 deletions(-) create mode 100644 bakerydemo/base/migrations/0004_auto_20170210_1445.py create mode 100644 bakerydemo/base/templatetags/gallery_tags.py create mode 100644 bakerydemo/templates/base/gallery_page.html create mode 100644 bakerydemo/templates/tags/gallery.html diff --git a/bakerydemo/base/migrations/0004_auto_20170210_1445.py b/bakerydemo/base/migrations/0004_auto_20170210_1445.py new file mode 100644 index 0000000..907d493 --- /dev/null +++ b/bakerydemo/base/migrations/0004_auto_20170210_1445.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.5 on 2017-02-10 14:45 +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('base', '0003_footertext'), + ] + + operations = [ + migrations.AlterModelOptions( + name='footertext', + options={'verbose_name_plural': 'Footer Text'}, + ), + ] diff --git a/bakerydemo/base/templatetags/gallery_tags.py b/bakerydemo/base/templatetags/gallery_tags.py new file mode 100644 index 0000000..b6f83ce --- /dev/null +++ b/bakerydemo/base/templatetags/gallery_tags.py @@ -0,0 +1,17 @@ +from django import template + +from wagtail.wagtailcore.models import Page +from wagtail.wagtailimages.models import Image + + +register = template.Library() + +# Retrieves a single gallery item and returns a gallery of images +@register.inclusion_tag('tags/gallery.html', takes_context=True) +def gallery(context, gallery): + images = Image.objects.filter(collection=gallery) + + return { + 'images': images, + 'request': context['request'], + } \ No newline at end of file diff --git a/bakerydemo/base/templatetags/navigation_tags.py b/bakerydemo/base/templatetags/navigation_tags.py index 23292f9..e3d5717 100644 --- a/bakerydemo/base/templatetags/navigation_tags.py +++ b/bakerydemo/base/templatetags/navigation_tags.py @@ -93,7 +93,9 @@ def breadcrumbs(context): @register.inclusion_tag('base/include/footer.html', takes_context=True) def get_footer_text(context): - footer_text = FooterText.objects.first().body + footer_text = "" + if FooterText.objects.first() is not None: + footer_text = FooterText.objects.first().body return { 'footer_text': footer_text, diff --git a/bakerydemo/breads/models.py b/bakerydemo/breads/models.py index 6ef298a..4419227 100644 --- a/bakerydemo/breads/models.py +++ b/bakerydemo/breads/models.py @@ -8,6 +8,8 @@ from wagtail.wagtailcore import blocks from wagtail.wagtailimages.edit_handlers import ImageChooserPanel from wagtail.wagtailsearch import index from wagtail.wagtailsnippets.models import register_snippet +from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger + @register_snippet @@ -105,7 +107,24 @@ class BreadsIndexPage(Page): def get_context(self, request): context = super(BreadsIndexPage, self).get_context(request) - context['breads'] = BreadPage.objects.descendant_of( - self).live().order_by( - '-first_published_at') + + # Get the full unpaginated listing of resource pages as a queryset - + # replace this with your own query as appropriate + all_resources = self.get_children().live() + + paginator = Paginator(all_resources, 5) # Show 5 resources per page + + page = request.GET.get('page') + try: + resources = paginator.page(page) + except PageNotAnInteger: + # If page is not an integer, deliver first page. + resources = paginator.page(1) + except EmptyPage: + # If page is out of range (e.g. 9999), deliver last page of results. + resources = paginator.page(paginator.num_pages) + + # make the variable 'resources' available on the template + context['resources'] = resources + context['paginator'] = paginator return context diff --git a/bakerydemo/templates/base/gallery_page.html b/bakerydemo/templates/base/gallery_page.html new file mode 100644 index 0000000..ef455e3 --- /dev/null +++ b/bakerydemo/templates/base/gallery_page.html @@ -0,0 +1,13 @@ +{% extends "base.html" %} +{% load wagtailimages_tags gallery_tags %} + +{% block content %} + {{ page.title }} + +
+ {% image page.image width-500 as photo %} + {{ photo.alt }} +
+ + {% gallery page.choices %} +{% endblock content %} diff --git a/bakerydemo/templates/breads/breads_index_page.html b/bakerydemo/templates/breads/breads_index_page.html index 8da6eee..1d39c2c 100644 --- a/bakerydemo/templates/breads/breads_index_page.html +++ b/bakerydemo/templates/breads/breads_index_page.html @@ -3,7 +3,38 @@ {% block content %} {{ page.title }} - {% for bread in breads %} -
{{ bread.title }}
- {% endfor %} + {% for bread in resources %} +
+ +
+ {% endfor %} + + {% if resources.has_other_pages %} +
+
+
    + {% if resources.has_previous %} +
  • + {% else %} +
  • + {% endif %} + {% for i in resources.paginator.page_range %} + {% if resources.number == i %} +
  • {{ i }} (current)
  • + {% else %} +
  • {{ i }}
  • + {% endif %} + {% endfor %} + {% if resources.has_next %} +
  • + {% else %} +
  • + {% endif %} +
+
+
+ {% endif %} + {% endblock content %} diff --git a/bakerydemo/templates/tags/gallery.html b/bakerydemo/templates/tags/gallery.html new file mode 100644 index 0000000..834804c --- /dev/null +++ b/bakerydemo/templates/tags/gallery.html @@ -0,0 +1,7 @@ +{% load wagtailimages_tags %} + +{% for img in images %} +{% image img max-285x200 as img_obj %} +
+{{ image.title }} +{% endfor %}