Python 想在我的主页上只显示第一篇最新文章吗
我试图在主页上只显示第一篇最新的文章,但仍然循环所有内容。求你了,我需要帮助 这是view.py:Python 想在我的主页上只显示第一篇最新文章吗,python,html,django,Python,Html,Django,我试图在主页上只显示第一篇最新的文章,但仍然循环所有内容。求你了,我需要帮助 这是view.py: def index(request): context = { 'infos': info.objects.all(), } return render(request, 'blog/index.html',context) index.html 我想知道如何仅显示从数据库发布的最新帖子 <div class="img-border">
def index(request):
context = {
'infos': info.objects.all(),
}
return render(request, 'blog/index.html',context)
index.html
我想知道如何仅显示从数据库发布的最新帖子
<div class="img-border">
<!-- {% for info in infos %} -->
<a href="{{ infos.video.url }}" class="popup-vimeo image-play">
<span class="icon-wrap">
<span class="icon icon-play"></span>
</span>
<img src="{% static 'blog/images/img_2.jpg' %}" alt="" class="img-fluid">
</a>
</div>
</div>
<div class="col-md-5 ml-auto">
<span class="caption px-0 text-muted">Latest Sermon</span>
<h2 class="h2 mb-0">{{ infos.title }}</h2>
<span class="d-block mb-3"><em>by</em> {{ infos.author }}</span>
<p class="h5 mb-2">If ye love Me, keep My Commandments.</p>
<p class="mb-4 word-wrap1">{{ infos.content }}</p>
<p><a href="{{ infos.video.url }}" class="popup-vimeo text-uppercase">Watch Video <span class="icon-arrow-right small"></span></a></p>
<!-- {% endfor %} -->
</div>
</div>
</div>
</div>
您只需更改查询即可:
def index(request):
context = {
'info': info.objects.order_by('-date_posted').first(),
}
return render(request, 'blog/index.html', context)
注:Toni Sredanović在其他答复中建议按发布日期订购,但
由于它默认为现在
,因此它将按照ID的顺序进行排序
可以简化为仅info.objects.last()
然后从模板中删除for
循环:
<!-- {% for info in infos %} -->
您只需更改查询即可:
def index(request):
context = {
'info': info.objects.order_by('-date_posted').first(),
}
return render(request, 'blog/index.html', context)
注:Toni Sredanović在其他答复中建议按发布日期订购,但
由于它默认为现在
,因此它将按照ID的顺序进行排序
可以简化为仅info.objects.last()
然后从模板中删除for
循环:
<!-- {% for info in infos %} -->
代替
info.objects.all()
使用info.objects.order\u by('-date\u posted')。first()
仅获取最新发布的info
对象。代替info.objects.all()
使用info.objects.order\u by('-date\u posted')。first()
仅获取最新发布的信息
对象。
是一个HTML注释,如果将Django命令放入其中,它们仍将被执行。尝试改用{%comment%}…{%endcomment%}
否则,当您只想显示第一个对象时,为什么要获取所有对象
如果我是你,我会这么做:
def索引(请求):
上下文={
“infos”:info.objects.filter(published\u is=True)。按('date\u posted')排序。first(),
}
返回呈现(请求'blog/index.html',上下文)
使用这个你当然不需要for循环
希望这有帮助
是一个HTML注释,如果您将Django命令放入其中,它们仍将被执行。尝试改用{%comment%}…{%endcomment%}
def index(request):
context = {'infos': info.objects.order_by('-date_posted').first(),}
return render(request, 'blog/index.html', context)
否则,当您只想显示第一个对象时,为什么要获取所有对象
如果我是你,我会这么做:
def索引(请求):
上下文={
“infos”:info.objects.filter(published\u is=True)。按('date\u posted')排序。first(),
}
返回呈现(请求'blog/index.html',上下文)
使用这个你当然不需要for循环
希望这有帮助 这回答了你的问题吗?info.objects.latest('date_posted')。这将提供根据发布日期的最新条目。此外,如果要自动添加当前时间而不是默认的时区,则应使用auto_add_now=True。这是否回答了您的问题?info.objects.latest('date_posted')。这将提供根据发布日期的最新条目。此外,如果要自动添加当前时间,而不是默认值=timezone.nowHey,则应使用auto_add_now=True!你刚才偷了我的答案吗DI在看到日期字段时添加了排序。我之前发布的所有内容都是xD。另外,由于日期默认为
时区。现在实际上不需要排序了-它将按照ID的顺序进行。@ToniSredanović我更新了帖子,这样我们两人都不会被冒犯,('date_posted')。可以使用last()
,但对我来说,理解起来似乎有点复杂(或者只是我过去常在字段和拳头上使用减号前缀)嘿!你刚才偷了我的答案吗?:DI在看到有日期字段时添加了排序。我之前发布的所有内容都是xD。另外,因为日期默认为时区。现在实际上不需要排序了-它将按照ID的顺序进行。@ToniSredanović我更新了帖子,所以我们两个都不应该被冒犯,('date_posted')。last()
可以使用,但对我来说,它似乎更难理解(或者只是我以前在字段和fists
call上使用减号前缀)
def index(request):
context = {'infos': info.objects.order_by('-date_posted').first(),}
return render(request, 'blog/index.html', context)