python - 在 django/python 我的主页中仅列出三个元素
问题描述
{% for post in BusinessPost %}
<ul>
<li>{{ post.title}} -- {{post.author}} -- {{post.source_date}} </li>
</ul>
{% endfor %}
这是我将模型的所有元素放入 listView 页面的代码,但我只想获取此列表的前 3 个元素。谁能帮忙
解决方案
您可以使用以下方法对查询集进行切片:
class BusinessPostListView(ListView):
# obtain the first 3 ↓
queryset = BusinessPost.objects.all()[:3]
# …
推荐阅读
- choco - 用 choco 在一个约束中对一个变量 (X[i][j]) 求和
- node.js - ECONNRESET 与 postgres 并在 nodejs 上进行续集
- c - 如何在两个数组中找到匹配的值?
- node.js - 在 save() 之前将 null 设置为 mongo 必填字段
- java - Spring Boot 应用程序在本地的 tomcat 8 上正常启动,但我得到 404
- python - 根据已知域查找完整的 URL
- ios - 无法将类型“(_)->()”的值转换为预期的参数类型“CompletionHandler?” 使用 KingFisher Swift
- sql - 用于搜索的 Oracle BLOB
- c - 像这样将 char 连接到 char 数组是否正确?
- node.js - 如何将 OpenLayers 5 与 Node (express) 一起使用?