как получить текущую вещь в моем фильтре и шаблоне django - PullRequest
0 голосов
/ 16 февраля 2011

это мои 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)

и это мой шаблон:

{% load  myfilters %}


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

но, это не может получить a.s,

так что я могу сделать,

спасибо

обновлен

это не тот же вопрос, потому что я буду использовать так:

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

1 Ответ

0 голосов
/ 16 февраля 2011

Для вашей ситуации я бы предложил сделать следующее:

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

Но для ответа на ваш вопрос используйте тег withhttp://docs.djangoproject.com/en/dev/ref/templates/builtins/#with

{% with a|return_element:forloop.counter0 as result %}
    {{ result|other_filter }}
{% endwith %}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...