Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在django过滤器和模板中获取当前内容_Python_Django_Templates_Filter - Fatal编程技术网

Python 如何在django过滤器和模板中获取当前内容

Python 如何在django过滤器和模板中获取当前内容,python,django,templates,filter,Python,Django,Templates,Filter,这是my views.py: a=[{'s':'sss'},{'s':'wwww'},{'s':'ssaawdw'},{'s':'qqqww'}] def main(request, template_name='index.html'): context ={ 'a':a, } return render_to_response(template_name, context) 这是我的过滤器: def return_next_elemen

这是my views.py:

a=[{'s':'sss'},{'s':'wwww'},{'s':'ssaawdw'},{'s':'qqqww'}]

def main(request, template_name='index.html'):
    context ={
              'a':a,
    }
    return render_to_response(template_name, context)
这是我的过滤器:

def return_next_element(list, index):
    if(index<len(list)-1):
        return list[index+1]
    else :
        return 0

register.filter('return_next_element',return_next_element)
但是,这不能得到a.s

那我该怎么办

谢谢

已更新

这不是同一个问题,因为我会这样使用:

a|return_element:forloop.counter0).s|other filter 

针对您的情况,我建议您采取以下措施:

{% for dictionary in a %}
     {% for key, value in dictionary.iteritems %}
          {{ key }}ww{{ value }}
     {% endfor %}
{% endfor %}
但要回答您的问题,请使用
with
标记。


您发布了两个不同问题的相同问题,
{% for dictionary in a %}
     {% for key, value in dictionary.iteritems %}
          {{ key }}ww{{ value }}
     {% endfor %}
{% endfor %}
{% with a|return_element:forloop.counter0 as result %}
    {{ result|other_filter }}
{% endwith %}