问题
I am trying to copy the elements of one list to another, and i am using this piece of code to do that:
{% for i in range(2,7) %}
{{ list1.append(list2|lookup:i) }}
{% endfor %}
list1 is an empty list. I have also defined this custom filter lookup like this:
@register.filter
def lookup(d, key):
return d[key]
But this does not work , I am getting this error:
TemplateSyntaxError at / expected token ',', got ':'.
What am i doing wrong. I am new to django and jinja template.
回答1:
The problem in your code is that you are trying to call the method append
with arguments which is not allowed in Django templates. You should write your own tag with the logic you need. For example:
list2|copy
回答2:
You can do it that filter with this simple lambda:
list1 + filter(lambda x: x if x not in list1 else None, range(2,7))
来源:https://stackoverflow.com/questions/45658280/referencing-a-particular-element-in-a-list-in-django