python - 如何在 django 模板的主页中显示简短版本的帖子
问题描述
我正在写一个博客应用程序,我想在主页上显示帖子的简短版本,在标题之后,每个帖子只有 4 行详细信息,然后有另一个帖子,但问题是所有显示在主页面中的详细信息页。
def tours(request):
featured = Tour.objects.filter(featured=True)
context = {
"object_list": featured,
}
return render(request, 'tours.html', context)
def sidebar(request):
return render(request, 'sidebar.html', {})
输出截图:
解决方案
可能您可以在模板中使用truncatechars
or trancatewords
。像这样:
{% for post in object_list %}
{{ post.detail|truncatechars:100 }} // assuming post.detail is the field where you write the body of the tours.
{% endfor %}
推荐阅读
- excel - 使用串联重命名文件名
- python - 在 numpy 中如何在内部转换值
- vba - 多行文本框将第一行设置为粗体
- asp.net - 更新面板不起作用:接收错误:“响应”不是有效的脚本名称。名称必须以“.js”结尾
- c# - 将 Appium 测试结果记录到控制台
- python - 有没有办法置换矩阵的子集?
- java - 使用 AJAX 在按钮单击事件后重新加载检票口组件
- laravel - 如何使用 Laravel 自定义包,称为 laravel-users?
- java - 如何修复“JAR 将为空 - 没有内容被标记为包含!” maven-jar-插件?
- python - Scrapy 规则、allowed_domains 回调和拒绝域的不同回调