gpt4 book ai didi

python - 我无法仅在模板中将 bool 字段设置为 true 时显示帖子

转载 作者:行者123 更新时间:2023-12-01 08:20:11 25 4
gpt4 key购买 nike

简而言之 - 我有一个引导轮播,它工作得很好,但是我无法让它仅显示“特色”设置为“true”的字段

我尝试过在 posts.objects.featured 中进行 for post (轮播实际上根本不会显示)和类似 posts.objects.filter(featured=True) 的变体 (它说它无法解析其余部分)。

这是模板中的代码,我尝试仅显示带有featured=True的项目的轮播图像


{% for post in posts.objects.featured %}
<div class="carousel-item {% if forloop.first %}active{% endif %} ">
{% image post.image fill-1920x500 %}
<div class="carousel-caption d-none d-md-block">
<h2 id="inner-carousel-title">{{post.title}}</h2>
<h4><a href="{% pageurl post %}" style="color:white;text-shadow:2px 2px 4px #000000" >something</a></h4>
</div>
</div>
{% endfor %}

同样,我只想轮播仅显示精选帖子顺便说一句 - 如果它只显示 3 个帖子,那就太棒了。

编辑 - 这是我的页面 model.py

    class BlogPage(RoutablePageMixin, Page):
description = models.CharField(max_length=240, blank=True)

content_panels = Page.content_panels + \
[FieldPanel("description", classname="full")]

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

最佳答案

如果您确实想在模板中执行此操作,请执行以下操作:

{% for post in posts %}
{% if post.featured %}
<div> ... <div/>
{% endif %}
{% endfor %}

但您也可以仅将精选帖子传递到 View 中的模板。只需添加:

...
featured_posts = Post.objects.filter(featured=True)[:4]
return render('post_list.html', {'featured_posts': featured_posts, ...})

如果您使用 Django 的通用 ListView 并且仅显示精选帖子,则可以设置 queryset 属性以仅过滤精选帖子。如果您还在 ListView 中显示其他帖子,请通过重写 get_context_data() 将featured_posts 添加到您的上下文中。

关于python - 我无法仅在模板中将 bool 字段设置为 true 时显示帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54697141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com