How to add, multiply number variables in a Django template?

前端 未结 5 1962
死守一世寂寞
死守一世寂寞 2020-12-28 19:36

The JS snippet I created relies on the forloop.counter variable being available within a {% for key, value in data.items %}..{% endfor %} tag.

Is there

5条回答
  •  余生分开走
    2020-12-28 20:15

    After Mathieu Marques tips, here's what I did using custom filter.

    The template is rendering 5 record per page after using pagination snippet. The per page presentation reset the forloop.counter. To create a continous counter per page..

    from django import template
    register = template.Library()
    
    @register.filter(name='cei')
    def compute_exact_id(value, rb_page_no):    
        new_id = value+(5*(rb_page_no-1))    ## here's the mathematical operation
        return new_id
    

    where rb_page_no is the current page number and placing {% load extra_filter %} on top my template where extra_filter is the file name where I place the compute_exact_id.

提交回复
热议问题