Get list item dynamically in django templates

前端 未结 5 1541
面向向阳花
面向向阳花 2020-11-28 09:48

I have some loop on the page and need list item depending from loop number.

When I call:

{{ mylist.1 }}
{{ mylist.2 }}
{{ mylist.3 }}
5条回答
  •  庸人自扰
    2020-11-28 10:50

    The slice tag in Django templates may use python's slicing code, but the syntax is unmistakably different. For instance, if you wanted to get an element of a sequence with a variable, in python you'd write something similar to the following:

    >>>mylist = ["0th Element", "1th Element"]
    >>>zero, one = 0, 1
    >>>mylist[zero]
    "0th Element"
    >>>mylist[one]
    "1th Element"
    

    Using this syntax with the Django slice template tag will return a sliced list in every case, of dubious utility for getting an item of known index:

    {% with "0" as zero %}
    {% with "1" as one %}
    {% with "2" as two %}
    
    {{mylist|slice:zero}} {{mylist|slice:one}} {{mylist|slice:two}}
    
    {% endwith %}
    {% endwith %}
    {% endwith %}
    

    Renders to the html:

    [] ["0th Element"] ["0th Element", "1th Element"]
    

    Note the differences: you are getting the result of mylist[:x] instead of mylist[x].

    Django provides enough tools to work around this. The first trick is to use explicit slices like 0:1 for your indices, and then |join:"" the resultant list into a single element. Like so:

    {% with "0:1" as zero %}
    {{mylist|slice:zero|join:""}}
    {% endwith %}
    

    Yields:

    0th Element
    

    This comes in particularly handy if you need to access a parent loop's index when dealing with an iterable inside a child loop:

    {% for parent in parent_loop %}
        {% cycle "0:1" "1:2" "2:3" as parent_loop_index silent %}
        {% for child in child_loop %}
            {{child|slice:parent_loop_index|join:""}}
        {% endfor %}
    {% endfor %}
    

    Completed with nothing but stock parts, although I don't think Django has implemented achievements yet.

提交回复
热议问题