Reputation: 11
I'm trying to get the hang of Wagtail and have a page that lists all of the posts on the site.
I want to display a category tab on the card that holds the info for each post.
The text and title are pulling correctly, but I can't get the category data.
I have this code, but it pulls every category on the site, not for each post.
{% for cat in categories %}
<li class="d-inline-flex">
<a href="{{self.get_parent.url}}?category={{cat.slug}}">
<span class="badge badge-pill badge-primary pt-2 pb-2">{{cat.name}}</span>
</a>
</li>
{% endfor %}
class LessonCategory(models.Model):
""" Set Up Lesson Category In Wagtail Admin """
name = models.CharField(max_length=100)
slug = models.SlugField(
unique=True,
max_length=40,
verbose_name="slug",
allow_unicode=True,
help_text="Add a category slug",
)
panels = [
FieldPanel('name'),
FieldPanel('slug'),
]
class Meta:
verbose_name = "Category"
verbose_name_plural = "Categories"
ordering = ["name"]
def __str__(self):
return self.name
Can anyone point me in the right direction?
Edit: Page List Model
class LessonListPage(RoutablePageMixin, Page):
""" Lists all the lessons on one page """
template = "lesson/lesson_list_page.html"
custom_title = models.CharField(
max_length=100,
blank=False,
null=False,
help_text='Overwrites the default title',
)
content_panels = Page.content_panels + [
FieldPanel("custom_title"),
]
def get_context(self, request, *args, **kwargs):
"""Adding custom stuff to our context"""
context = super().get_context(request, *args, **kwargs)
context["posts"] = LessonDetailPage.objects.live().public()
#Change 'public' to adjust which posts are displayed
context["categories"] = LessonCategory.objects.all()
return context
@route(r'^latest/$')
def latest_lesson_posts(self, request, *args, **kwargs):
context = self.get_context(request, *args, **kwargs)
# @todo Adjust the number of lessons that are displayed at one time
context["latest_lessons"] = LessonDetailPage.objects.live().public()[:10]
return render(request, "lesson/latest_lessons.html", context)
Upvotes: 1
Views: 576
Reputation: 11
{% for cat in post.categories.all %}
<li class="d-inline-flex">
<a href="{{cat.get_parent.url}}?category={{cat.slug}}">
<span class="badge badge-pill badge-primary pt-2 pb-2">
{{cat.name}}
</span>
</a>
</li>
{% endfor %}
Changing 'categories' --> 'post.categories.all' did the trick.
Upvotes: 0