Great tutorial, especially working out the filtering by category. Immensely useful :) Probably wouldn't be trusting content editors to make slugs though 😂 I went with the autoslug field from django extensions: from django_extensions.db.fields import AutoSlugField ... slug = AutoSlugField( populate_from=['name'], verbose_name='slug', allow_unicode=True, max_length=200, ) then drop slug from the panel
Nope I don't believe so. But it'd be as easy as BlogDetailPage.objects.live().public().filter(categories__slug__in=[request.GET.get('category')]), or something along these lines. (Note: that's untested, so you may need to adjust it)
I applied the filter my side, I used this: # Get all the blog posts. posts = BlogDetailPage.objects.live().public() # Identify recent posts and send to template: context["recent_posts"] = posts[:5] # Check if there is a category slug in the URL to filter the blog posts by. category_slug = request.GET.get("category", None) context["blog_posts"] = posts # By default - set to all posts if category_slug: # Must have a count > 0 in order to display something if posts.filter(categories__slug__contains=category_slug).count() > 0: context["blog_posts"] = posts.filter(categories__slug__contains=category_slug) else: context["blog_posts"] = posts # Seems like repetition but it caters for the 0 count on a blog_slug. ** I used the context["recent_posts"] to create a latest 5 posts navigation on the right of my content. Reverse ordered in template. ** I used the context["blog_posts"] to actually filter the blog list view when certain categories were clicked. (Only displayed categories with more than 0 posts associated to them)
@@DouglasMcKey using categories__slug__contains=category_slug doesn't work if the name of the category forms part of the name of other categories. It'll pick up all those categories instead of just the one you want. I tried this with having Education, Early Education, Environmental Education. When I went with ?=Education I got all 3 categories. Just categories__slug=category_slug works fine though.
You're awesome! the way you teach is the one way that I'm exciting to follow and in fact, I don't sleep during the video :))
(▀Ĺ̯▀ ̿) Thank you!! :D
Great tutorial and amazing instructor :)
I click like button before I watch the videos !!
Amazing! Thank you so much for the support!
Is there any way to use tag chooser panel style ? Can't find anything about it.
Great tutorial, especially working out the filtering by category. Immensely useful :)
Probably wouldn't be trusting content editors to make slugs though 😂 I went with the autoslug field from django extensions:
from django_extensions.db.fields import AutoSlugField
...
slug = AutoSlugField(
populate_from=['name'],
verbose_name='slug',
allow_unicode=True,
max_length=200,
)
then drop slug from the panel
filter inspired by Kalob
if request.GET.get('category'):
context["posts"] = BlogDetailPage.objects.live().public().filter(categories__slug__in=[request.GET.get('category')])
else:
context["posts"] = BlogDetailPage.objects.live().public()
@15:45 Have you, eventually, implemented the filter?
Nope I don't believe so. But it'd be as easy as BlogDetailPage.objects.live().public().filter(categories__slug__in=[request.GET.get('category')]), or something along these lines. (Note: that's untested, so you may need to adjust it)
I applied the filter my side, I used this:
# Get all the blog posts.
posts = BlogDetailPage.objects.live().public()
# Identify recent posts and send to template:
context["recent_posts"] = posts[:5]
# Check if there is a category slug in the URL to filter the blog posts by.
category_slug = request.GET.get("category", None)
context["blog_posts"] = posts
# By default - set to all posts
if category_slug:
# Must have a count > 0 in order to display something
if posts.filter(categories__slug__contains=category_slug).count() > 0:
context["blog_posts"] = posts.filter(categories__slug__contains=category_slug)
else:
context["blog_posts"] = posts # Seems like repetition but it caters for the 0 count on a blog_slug.
** I used the context["recent_posts"] to create a latest 5 posts navigation on the right of my content. Reverse ordered in template.
** I used the context["blog_posts"] to actually filter the blog list view when certain categories were clicked. (Only displayed categories with more than 0 posts associated to them)
@@DouglasMcKey using categories__slug__contains=category_slug doesn't work if the name of the category forms part of the name of other categories. It'll pick up all those categories instead of just the one you want. I tried this with having Education, Early Education, Environmental Education. When I went with ?=Education I got all 3 categories. Just categories__slug=category_slug works fine though.