0

我使用从表中查询日期

  author=Author.objects.all() 

并且在模板中只显示了变量{{ author }}。所以如果表中有 10 个项目,我不想显示所有 10 个项目,而是我需要显示1st 3 items并指示更多可用 3dots 的数据(...)

要求是这样的Python,Django,Pycharm...

这个怎么做。

4

1 回答 1

3
authors = Author.objects.all()

{% for author in authors|slice:":3" %}
    {{ author }}{% if not forloop.last %},{% endfor %}
{% endfor %}
{% if authors|length > 3 %}...{% else %}.{% endif %}

或者只是首先对查询集进行切片

authors = Author.objects.all()[:3]

{% for author in authors %}{{ author }}{% endfor %}... 
于 2013-05-31T13:43:35.327 回答