Django模板将操作结果保存到变量中

时间:2013-07-23 10:25:04

标签: django django-templates

我有一个列表计数器“2370”,页面只显示“12”项,所以我想计算页码,我做了下面的解决方案

{% widthratio listing.total_count 12 1 %}

那么,我如何将结果保存到变量中呢?

{% set total_pages =  widthratio listing.total_count 12 1 %}

这个没有用

1 个答案:

答案 0 :(得分:0)

如果选择Rohans评论,但如果您要编写自己的模板标签,请使用assignment_taghttps://docs.djangoproject.com/en/1.7/howto/custom-template-tags/#assignment-tags,自Django 1.4以来存在。)

@register.assignment_tag
def page_numbers(total_items, items_per_page):
    if not total_items:
         return 0
    # int divisions round down (1 / 12 = 0), so just add 1 to cover this 
    return total_items / items_per_page + 1

您应该使用的模板中;

{% page_numbers listing.total_count 12 as page_nrs %}