引用Django列表中的特定元素 [英] Referencing a particular element in a list in django

查看:80
本文介绍了引用Django列表中的特定元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个列表的元素复制到另一个列表,并且我正在使用这段代码来做到这一点:

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是一个空列表. 我还定义了这个自定义过滤器查找,如下所示:

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 ':'.

我在做什么错.我是django和jinja模板的新手.

What am i doing wrong. I am new to django and jinja template.

推荐答案

代码中的问题是您尝试使用Django模板中不允许的参数调用方法append.您应该使用所需的逻辑编写自己的标签.例如:

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

这篇关于引用Django列表中的特定元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆