g:如何在列表页面上显示子页面的类别

时间:2020-06-24 16:46:50

标签: django wagtail

我试图摆脱Wagtail的困扰,并提供一个页面,列出该网站上的所有帖子。

我想在卡片上显示一个类别标签,其中包含每个帖子的信息。

文本和标题正确提取,但是我无法获取类别数据。

我有这段代码,但是它提取了网站上的每个类别,而不是每个帖子。

              {% 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

有人能指出我正确的方向吗?

编辑:页面列表模型

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)

1 个答案:

答案 0 :(得分:0)

.showme {
    display: none;
}

.showhim:hover ~ .showme {
    display: block;
}

更改“类别”->“ post.categories.all”可以解决问题。