Python 我能';t仅在我的模板中将布尔字段设置为true时显示帖子
简言之,我有一个引导转盘,它工作得很好,但是我不能让它只显示“featured”设置为“true”的字段 我尝试过在posts.objects.featured中发布Python 我能';t仅在我的模板中将布尔字段设置为true时显示帖子,python,html,django,wagtail,Python,Html,Django,Wagtail,简言之,我有一个引导转盘,它工作得很好,但是我不能让它只显示“featured”设置为“true”的字段 我尝试过在posts.objects.featured中发布(旋转木马实际上根本不显示)和类似posts.objects.filter(featured=True)(它说它无法解析其余部分)的变体 下面是模板中的代码,我试图仅显示带有featured=True的项目的旋转木马图像 {% for post in posts.objects.featured %} <div clas
(旋转木马实际上根本不显示)和类似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 %}
如果我在评论中建议的方式不起作用,你可以试试这个
{% for post in posts %}
{% if post.featured %}
// write down your stuff
{% endif %}
{% endfor %}
如果确实要在模板中执行此操作,请执行以下操作:
{% for post in posts %}
{% if post.featured %}
<div> ... <div/>
{% endif %}
{% endfor %}
如果您使用的是Django的genericListView
,并且只显示特色文章,那么可以设置queryset
属性,仅过滤特色文章。如果您还在ListView中显示其他帖子,请通过覆盖get\u context\u data()
将特色帖子添加到您的上下文中 试着只写帖子。功能在哪里定义了self.posts
?我怀疑问题在于,这会给你一组普通的页面
实例,而不是定义了特色
标志的特定页面模型-因此,按该标志过滤将不起作用。那么我在哪里做特色文章呢?我说:在你看来。我不知道您是如何构建视图(函数视图或CBV)的,所以我只是建议您可以采用的方法。这取决于你的观点。
{% for post in posts %}
{% if post.featured %}
<div> ... <div/>
{% endif %}
{% endfor %}
...
featured_posts = Post.objects.filter(featured=True)[:4]
return render('post_list.html', {'featured_posts': featured_posts, ...})