1
Fork 0

Refactor tag route to standard querystring on view

This commit is contained in:
Jake Howard 2018-08-03 20:28:10 +01:00
parent 670efc0928
commit 078af3e8f5
Signed by: jake
GPG Key ID: 57AFB45680EDD477
4 changed files with 10 additions and 13 deletions

View File

@ -39,7 +39,7 @@ class BlogPage(Entity):
return self.post_date.strftime("%Y-%m")
class BlogIndexPage(RoutablePageMixin, Entity):
class BlogIndexPage(Entity):
body = build_stream_field()
subpage_types = [BlogPage]
@ -48,16 +48,12 @@ class BlogIndexPage(RoutablePageMixin, Entity):
StreamFieldPanel('body')
]
@route(r'^tag/(\w+)/$')
def tags(self, request, *args, **kwargs):
self.tag_filter = args[0]
return Entity.serve(self, request, *args, **kwargs)
def get_context(self, request, *args, **kwargs):
context = super().get_context(request)
child_pages = self.get_children().specific().live().values_list('id', flat=True)
context['blogs'] = BlogPage.objects.filter(id__in=child_pages)
if hasattr(self, 'tag_filter'):
context['tag'] = self.tag_filter
context['blogs'] = context['blogs'].filter(tags__name=self.tag_filter)
tag_filter = request.GET.get('tag')
if tag_filter:
context['tag'] = tag_filter
context['blogs'] = context['blogs'].filter(tags__name=tag_filter)
return context

View File

@ -49,7 +49,6 @@ INSTALLED_APPS = [
'wagtail.contrib.forms',
'wagtail.contrib.redirects',
'wagtail.contrib.routable_page',
'wagtail.embeds',
'wagtail.sites',
'wagtail.users',

View File

@ -1,6 +1,6 @@
{% extends "base.html" %}
{% load static wagtailimages_tags %}
{% load static wagtailimages_tags wagtailcore_tags %}
{% block content %}
<div id="main">
@ -11,7 +11,7 @@
<div class="mb-3 image header-image" data-image='{{ photo.url }}'></div>
{% endif %}
<h1 class="display-5">{{ page.title }}</h1>
<h5 class="my-3">Posts tagged with <a href="">#{{ tag }}</a></h5>
<h5 class="my-3">Posts tagged with <a href="{% slugurl "blog" %}?tag={{ tag }}">#{{ tag }}</a></h5>
{% else %}
{% include "common/content.html" %}
{% endif %}

View File

@ -1,9 +1,11 @@
{% load wagtailcore_tags %}
{% if page.post_date %}
<span class="pr-1" title='{{ page.post_date }}'>{{ page.post_date }}</span>
{% endif %}
{% if page.tags.all.exists %}
{% for tag in page.tags.all %}
<a href="" class="mr-1">#{{ tag }}</a>
<a href="{% slugurl "blog" %}?tag={{ tag }}" class="mr-1">#{{ tag }}</a>
{% endfor %}
{% endif %}