在模板-Dango中显示限制项目

时间:2013-05-31 13:36:53

标签: django django-templates

我使用

查询了表中的日期
  author=Author.objects.all() 

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

必需与此Python,Django,Pycharm...

相同

如何做到这一点。

1 个答案:

答案 0 :(得分:3)

authors = Author.objects.all()

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

或者首先只是slice查询集:

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

{% for author in authors %}{{ author }}{% endfor %}...