Add messages to base.html, handle /tags/ and /tags/[bad-slug]/

pull/2/head
David Ray 2017-02-10 07:10:49 -05:00
rodzic 68ca56d640
commit c428998049
2 zmienionych plików z 24 dodań i 6 usunięć

Wyświetl plik

@ -1,7 +1,8 @@
from __future__ import unicode_literals
from django.contrib import messages
from django.db import models
from django.shortcuts import get_list_or_404, get_object_or_404, render
from django.shortcuts import redirect, render
from modelcluster.fields import ParentalKey
from modelcluster.contrib.taggit import ClusterTaggableManager
@ -150,16 +151,23 @@ class BlogIndexPage(RoutablePageMixin, Page):
'-first_published_at')
return context
@route('^tags/$', name='tag_archive')
@route('^tags/(\w+)/$', name='tag_archive')
def tag_archive(self, request, tag=None):
'''
A Custom view that utilizes Tags. This view will
return all related BlogPages for a given Tag.
return all related BlogPages for a given Tag or redirect back to
the BlogIndexPage
'''
tag = get_object_or_404(Tag, slug=tag)
blogs = get_list_or_404(
BlogPage.objects.filter(tags=tag).live().descendant_of(self)
)
try:
tag = Tag.objects.get(slug=tag)
except Tag.DoesNotExist:
if tag:
msg = 'There are no blog posts tagged with "{}"'.format(tag)
messages.add_message(request, messages.INFO, msg)
return redirect(self.url)
blogs = BlogPage.objects.filter(tags=tag).live().descendant_of(self)
context = {
'title': 'Posts tagged with: {}'.format(tag.name),

Wyświetl plik

@ -112,6 +112,16 @@
{# breadcrumbs is defined in base/templatetags/navigation_tags.py #}
{% endblock %}
{% if messages %}
<div>
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
{% block content %}
{% endblock %}