Python 过滤django新闻帖子
我是Django的新手,我有一篇新闻文章,在同一个模板的右侧有一个部分,显示了所有最新的文章。然而,当你在一个主要的新闻帖子上时,它也会出现在右边的“最新新闻”标签上 我很确定我需要使用.exclude来过滤掉正在显示的内容。然而,我不知道django如何知道显示的是哪个帖子 如果您需要查看我的代码,请询问。我只使用基本模型/视图来输出数据 显示最新3篇文章的行:Python 过滤django新闻帖子,python,django,Python,Django,我是Django的新手,我有一篇新闻文章,在同一个模板的右侧有一个部分,显示了所有最新的文章。然而,当你在一个主要的新闻帖子上时,它也会出现在右边的“最新新闻”标签上 我很确定我需要使用.exclude来过滤掉正在显示的内容。然而,我不知道django如何知道显示的是哪个帖子 如果您需要查看我的代码,请询问。我只使用基本模型/视图来输出数据 显示最新3篇文章的行: other_news = NewsPost.objects.filter(live=True, categories__in=pos
other_news = NewsPost.objects.filter(live=True, categories__in=post.categories.all).distinct().order_by("-posted")[:3]
模板的代码:
<div class='related_article_wrapper'>
{% if other_news %}
{% for news in other_news %}
<div class="article_snipppet_wrap">
<img class="article_icon" src="/media/images/article_icon.png" alt="" />
<p>{{news.title}}</p>
<span><a href="{{news.get_absolute_url}}">{{news.posted|date:"d/m/y"}} »</a></span>
</div>
{% endfor %}
<span><a style="text-decoration: none; href="/news-hub/news/">View all news »</a></span>
{% endif %}
</div>
{%if-other_news%}
{其他新闻中的新闻%}
{{news.title}
{%endfor%}
{%endif%}
谢谢
Josh只需将.exclude(id=post.id)
添加到您的筛选链:
other_news = NewsPost.objects.exclude(id=post.id).filter(live=True,
categories__in=post.categories.all).distinct().order_by("-posted")[:3]
采用与
filter()
相同格式的参数,它的作用正好相反 请给我们看一些你的相关代码!对非常感谢!真的很感激!大量帮助!:)