开发者

how to get the current thing in my django filter and template

开发者 https://www.devze.com 2023-02-10 15:17 出处:网络
this is my views.py : a=[{\'s\':\'sss\'},{\'s\':\'wwww\'},{\'s\':\'ssaawdw\'},{\'s\':\'qqqww\'}] def main(request, template_name=\'index.html\'):

this is 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)

this is my filter :

def return_next_element(l开发者_运维问答ist, index):
    if(index<len(list)-1):
        return list[index+1]
    else :
        return 0

register.filter('return_next_element',return_next_element)

and this is my template :

{% load  myfilters %}


{% for i in a %}
    {{ i }}ww{{ (a|return_element:forloop.counter0).s }}
{% endfor %}

but , this cant get the a.s ,

so what can i do ,

thanks

updated

this is not a same question , because i will use like this :

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


For your situation, I'd suggest doing the following:

{% for dictionary in a %}
     {% for key, value in dictionary.iteritems %}
          {{ key }}ww{{ value }}
     {% endfor %}
{% endfor %}

But to answer your question, use the with tag. http://docs.djangoproject.com/en/dev/ref/templates/builtins/#with

{% with a|return_element:forloop.counter0 as result %}
    {{ result|other_filter }}
{% endwith %}
0

精彩评论

暂无评论...
验证码 换一张
取 消