django slice numbers in template
Asked Answered
V

4

16

Is there a way to get multiple digits of a given number within a django template?

For example:

{{ some_num|get_digit:2 }} 

will give you the second right most digit. For 1224531 it would be 3

Is there a way to get the last 3 digits or the first 5 digits? Like python's slicing?

something like:

{{ some_num|get_digits:2,5}}
Visage answered 11/1, 2012 at 23:28 Comment(1)
searching for "django template tag slice" would have brought you to the answer!Whitewall
W
42

There is a the "slice" template tag

https://docs.djangoproject.com/en/dev/ref/templates/builtins/#slice

It uses the same syntax as Python's list slicing.

Example:

{{ some_list|slice:":2" }}

in python this is equivalent to:

some_list[:2]

BTW your 2nd example would be "2:5" not "2,5"

NB. Python slicing works on any 'sequence'. Strings and lists are sequences. Numbers are not!

Whitewall answered 11/1, 2012 at 23:30 Comment(4)
but does this mean i'd have to convert the number to a list first?Visage
@Visage : the slice tag works like python's slice: a string is iterable, so it works perfectly on string. It does not work on numbers - but again, python's slice does not work on numbers either.Whitewall
does not work on dynamic values - i.e. where index of slicing comes from a variable. In Jinja2 templates you just do {{ some_list[:upper_index] }} and it works.Whimsey
What would be the behavior in a for loop? For instance, looping posts from a model in |slice: 4 ? Always the first id will be displayed in this case? I am working in a issue where I need to loop four items and remove the first id or id=1 but it seen that based on id the loop is shuffled. So, the first id will always be retrieved?Imperative
M
6

any extra filter that converts the number into a string before slicing will work. I used these variants:

{{ some_num|slugify|slice:"2:5" }}

and

{{ some_num|stringformat:"d"|slice:"5:10" }}
Materi answered 5/6, 2013 at 11:13 Comment(0)
K
3

{{1234567|make_list|slice:'2:5'|join:''}}

Stefano's answer is on the right track. You need a pre-processing step to turn your number into a list, and a post-processing step to merge that list back into string.

Khalilahkhalin answered 3/3, 2013 at 14:31 Comment(0)
E
0

You just need to write code as follow for slicing :- {{valueformoney|slice:"0:4"}}

{% for cloth in valueformoney|slice:"0:4" %}
    <div class="product h-100  w-100 border rounded ">
        <div class="img__container">
            <img src="{{cloth.cloth_image.url}}" alt="" />
        </div>
        <div class="product__bottom">
            <div class="price">
                <span class="text-danger"> <del>{% min_price cloth as result %} {{ result|rupee}}</del></span>&nbsp;
                <span>{% discount_price cloth as result %}{{result|rupee}}</span>
                <span class="float-right badge p-3 badge-info">Save {{cloth.cloth_discount}}% </span>
            </div>
            <h3 class="p-4">{{cloth.cloth_name}}</h3>
            <div class="button">
                <a href="/product_detail/{{cloth.cloth_slug}}" class="btn-block">See More</a>
            </div>
        </div>
    </div>
{% endfor %}
Evenhanded answered 11/1, 2021 at 16:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.