0

I fail to display the routable index page for categories/given category.

My code uses: -BlogCategory(model)

-BlogPageBlogCategory (page) an intemediary structure that links to:

-PostPage(page)

I can pass the code to the post_page template, but when I click on a specific category link I get:

Reverse for 'post_by_category' with arguments '('',)' not found. 1 pattern(s) tried: ['category/(?P<category>[-\\w]+)/$']

In the following post @gasman wrote: "the routablepageurl tag has received an empty string". I couldn't find the 'empty' string/missing link.

I assume it's related to the route of my 'def post_by_category'. Any input that would help me deepen my learning woulg be great.

NB - in case it helps, when I run this procedure without the intemeiary page all's fine. I can display the BlogPage/given_category once I click on the category in the PostPage.

Here's my code:

Models

class BlogPage(RoutablePageMixin, Page):
...


    def get_context(self, request, *args, **kwargs):
        context = super().get_context(request, *args, **kwargs)
        context["posts"] = posts
        return context

    def get_posts(self):
        return PostPage.objects.descendant_of(self).live().order_by("-post_date")

@route(r'^category/(?P<category>[-\w]+)/$')
    def post_by_category(self, request, category, *args, **kwargs):
        self.posts =  self.get_posts().filter(categories__blog_category__slug=category)
        context["categories"] = BlogCategory.objects.all()
        return self.render(request)


class PostPage(MetadataPageMixin, Page):
...
    content_panels = Page.content_panels + [
...
    InlinePanel("categories", label="category"),
...

def get_context(self, request, *args, **kwargs):
        context = super().get_context(request, *args, **kwargs)
        return context
        
class PostPageBlogCategory(models.Model):
    page = ParentalKey(
        "blog.PostPage", on_delete=models.CASCADE, related_name="categories"
    )
    blog_category = models.ForeignKey(
        "blog.BlogCategory", on_delete=models.CASCADE, related_name="post_pages"
    )

    panels = [
        SnippetChooserPanel("blog_category"),
    ]

    class Meta:
        unique_together = ("page", "blog_category")

@register_snippet
class BlogCategory(models.Model):
    name = models.CharField(max_length=255)
    slug = models.SlugField(unique=True, max_length=80)

    panels = [
        FieldPanel('name'),
        FieldPanel("slug"),
    ]

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = 'categories'

The post_page.html:

{% extends "base.html" %}
{% load wagtailroutablepage_tags %}

...
{% for category in page.categories.all %}
    <li>
        <a href="{% routablepageurl blog_page "post_by_category" category.slug %}">
            {{ blog_category.name }}
        </a>
    </li>
    {% empty %}
        No categories yet'
{% endfor %}

...
donbonbon
  • 81
  • 1
  • 8
  • I suggest you start debugging by removing the `{% routablepageurl %}` tag from the post_page.html template, and outputting `{{ category.slug }}` directly instead. If - as I suspect - it's an empty string, then you have a bug to investigate that isn't related to RoutablePage. – gasman Jan 20 '22 at 12:37

1 Answers1

0

After debugging and testing, I didn't find any blank categories, (which doesn't mean there were none, but that I didn't find or deleted them Unintentionally). I case it could help, what worked for me was adding the category_type at the beginning of my varaiable:

{{ category.blog_category.name }} rather than {{ blog_category.name }}

        {% for category in page.categories.all %}

        <a href="{% routablepageurl article_index_page "post_by_category" category.blog_category.slug %}" class="link-primary text-decoration-none">
        {{ category.blog_category.name }}
        </a>

         {% empty %}
         No categories yet'
        {% endfor %}
donbonbon
  • 81
  • 1
  • 8