Events by category with CBV and slugs

Hi! I’m completely confused. I need all events belonging to certain category to be displayed when clicking on the category name. So simple. Like /category/dogs displays all posts about dogs.
Please help me with this difficult situation! I cant get right queryset :frowning:

urlpatterns = [
    path('', HomePageView.as_view(), name='home'),
    path('events/<slug:slug>', EventDetailView.as_view(), name='event_detail'),
    path('category/<slug:slug>/', EventsByCategory.as_view(), name='events_by_category'),
    path('about/', AboutPageView.as_view(), name='about'),

My models are:

from django.db import models
from django.utils import timezone
from django.urls import reverse

class Category(models.Model):
    title = models.CharField(max_length=100, verbose_name='')
    slug = models.SlugField(max_length=100, verbose_name='')

    class Meta:
        verbose_name = ''
        verbose_name_plural = ''
        ordering = ['title']
        indexes = [

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        # return reverse('events_by_category', args=[self.slug])
        return reverse('events_by_category', kwargs = {'slug': self.slug})

class Event(models.Model):
    class Status(models.TextChoices):
        DRAFT = 'DF', 'Draft'
        PUBLISHED = 'PB', 'Published'

    title = models.CharField(max_length=250, verbose_name='')
    body = models.TextField(verbose_name='')
    slug = models.SlugField(unique=True, verbose_name='')
    image = models.ImageField(upload_to='media/%Y/%m/%d', blank=True, verbose_name='')
    category = models.ForeignKey(Category, on_delete=models.PROTECT, related_name='events', verbose_name='')
    created = models.DateTimeField(, verbose_name='')
    status = models.CharField(max_length=2, choices=Status.choices, default=Status.DRAFT, verbose_name='')

    class Meta:
        verbose_name = ''
        verbose_name_plural = ''
        ordering = ['-created']
        indexes = [

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('event_detail', args=[self.slug])

And my shame is the file uncut:

# from django.shortcuts import get_object_or_404
from django.views.generic import ListView, DetailView, TemplateView
from .models import Event, Category

class HomePageView(ListView):
    queryset = Event.objects.filter(status=Event.Status.PUBLISHED)
    context_object_name = 'events'
    template_name = 'home.html'
    paginate_by = 3

    # def get_context_data(self, *, object_list=None, **kwargs):
    #     context = super().get_context_data(**kwargs)
    #     context['category'] =
    #     return context

class EventDetailView(DetailView):
    model = Event
    template_name = 'event_detail.html'
    context_object_name = 'event'

class EventsByCategory(ListView):
    # queryset = Category.objects.filter(slug=slug, status=Event.Status.PUBLISHED)
    model = Event
    template_name = 'home.html'
    context_object_name = 'events'
    paginate_by = 3
    category = Category.objects.all()

    def get_queryset(self):
        # category = get_object_or_404(Category, slug=self.kwargs.get('slug'))
        # return Category.objects.filter(slug=self.kwargs.get('slug'), status=Event.Status.PUBLISHED)
        category = get_object_or_404(Category, slug=self.kwargs.get('slug'))
        return category.objects.all()

class AboutPageView(TemplateView):
    template_name = 'about.html'

Side note: When posting code here, please surround the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted. You don’t need to repost your code, you can edit this post and add the ``` before and after each file’s code.

See the docs at Making queries | Django documentation | Django for how to query for all the elements through the “reverse” of a ForeignKey relationship.

Ah-ha, thanks a lot!
I’ve done it with FBV so far. There is still confusion in my head about how to implement this in the ListView class. Here’s how it works for me (I know there is no 404 processing)

def events_by_category(request, slug):
    category = Category.objects.get(slug=slug)
    events =
    return render(request, 'home.html', {'events': events, 'category': category})