rosslaird
rosslaird

Reputation: 39

How to show the category name on the category listing page?

I've been using Wagtail for a while, for my basic website, but during the pandemic I've had to create online versions of all my courses -- I teach at a university -- and I've decided to do this in Wagtail. It's a much more robust solution than anything the university can provide. (I think a lot of people are in my situation.) It's been great so far, but I'm not a programmer (I teach Creative Writing!) and so I'm a bit out of my depth with a few things. Here's one thing I could use help with. I have categories (as per Kalob's tutorials), and I have category listing pages. I click on a category and I get a page showing just pages in that category. So far so good. But how would I show, on these listing pages, a header that says "Showing Pages in Category X"? Here's what I have in the models for my courses app (taken straight from Kalob's tutorial on categories):

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

    try:
        # Look for the course category by its slug.
        category = CourseCategory.objects.get(slug=cat_slug)
    except Exception:
        category = None

    if category is None:
        # This is an additional check.
        pass

    context["courses"] = (
        CoursePage.objects.live().public().filter(
            categories__in=[category])
    )

And here's what I have in the template for these category pages:

                   <ul class="menu-list">
                        {% for cat in categories %}
                        <li>
                            <a href="{% routablepageurl page "category_view" cat.slug %}">
                                {{ cat.name }}
                            </a>
                        </li>
                        {% endfor %}
                    </ul>
           

As Intended, the above shows all categories. How would I show only the category of the selected pages, so that I could put that in the title of the page? I've tried a few things, but as I said, I'm out of my depth here and I am just guessing and googling -- so far, without success. I know a lot of the ways to not make this work.

Upvotes: 0

Views: 260

Answers (1)

LB Ben Johnston
LB Ben Johnston

Reputation: 5186

Not sure if I am missing something, but if you want to make the current category available to your template (the one based on the slug in the URL). You can add it to the context to make it available in your template, similar to how you are adding courses.

Example

my_model.py
@route(r"^category/(?P<cat_slug>[-\w]*)/$", name="category_view")
def category_view(self, request, cat_slug):
    """Find courses based on a category."""
    # ... rest of the code from above

    context["courses"] = (
        CoursePage.objects.live().public().filter(
            categories__in=[category])
    )
    context["current_category"] = category
my_template.html
<h2>{{ page.title }} - {{ current_category }}

Upvotes: 1

Related Questions