Skip to content
Permalink
Browse files

Category filter views with Routable Pages

  • Loading branch information...
KalobTaulien committed Jul 8, 2019
1 parent 261adec commit 1e21b345cd4f8cdfbc1a3fdd9bb53234aaacb820
Showing with 43 additions and 1 deletion.
  1. +42 −0 blog/models.py
  2. +1 −1 mysite/templates/blog/blog_listing_page.html
@@ -202,6 +202,48 @@ def get_context(self, request, *args, **kwargs):
context["categories"] = BlogCategory.objects.all()
return context

@route(r"^july-2019/$", name="july_2019")
@route(r"^year/(\d+)/(\d+)/$", name="blogs_by_year")
def blogs_by_year(self, request, year=None, month=None):
context = self.get_context(request)
# Implement your BlogDetailPage filter. Maybe something like this:
# if year is not None and month is not None:
# posts = BlogDetailPage.objects.live().public().filter(year=year, month=month)
# else:
# # No year and no month were set, assume this is july-2019 only posts
# posts = BlogDetailPage.objects.live().public().filter(year=2019, month=07)
# print(year)
# print(month)
# context["posts"] = posts

# Note: The below template (latest_posts.html) will need to be adjusted
return render(request, "blog/latest_posts.html", context)

@route(r"^category/(?P<cat_slug>[-\w]*)/$", name="category_view")
def category_view(self, request, cat_slug):
"""Find blog posts based on a category."""
context = self.get_context(request)

try:
# Look for the blog category by its slug.
category = BlogCategory.objects.get(slug=cat_slug)
except Exception:
# Blog category doesnt exist (ie /blog/category/missing-category/)
# Redirect to self.url, return a 404.. that's up to you!
category = None

if category is None:
# This is an additional check.
# If the category is None, do something. Maybe default to a particular category.
# Or redirect the user to /blog/ ¯\_(ツ)_/¯
pass

context["posts"] = BlogDetailPage.objects.live().public().filter(categories__in=[category])

# Note: The below template (latest_posts.html) will need to be adjusted
return render(request, "blog/latest_posts.html", context)


@route(r'^latest/$', name="latest_posts")
def latest_blog_posts_only_shows_last_5(self, request, *args, **kwargs):
context = self.get_context(request, *args, **kwargs)
@@ -10,7 +10,7 @@ <h2>
Categories:
<small>
{% for cat in categories %}
<a href="?category={{ cat.slug }}">
<a href="{% routablepageurl page "category_view" cat.slug %}">
{{ cat.name }}
</a>{% if not forloop.last %}, {% endif %}
{% endfor %}

0 comments on commit 1e21b34

Please sign in to comment.
You can’t perform that action at this time.