【问题标题】:how to get the current thing in my django filter and template如何在我的 django 过滤器和模板中获取当前内容
【发布时间】:2011-02-16 10:52:40
【问题描述】:

这是我的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 

【问题讨论】:

  • 你两次发布同一个问题
  • 那不是同一个问题,

标签: python django templates filter


【解决方案1】:

对于您的情况,我建议您执行以下操作:

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

但要回答您的问题,请使用with 标签。 http://docs.djangoproject.com/en/dev/ref/templates/builtins/#with

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

【讨论】:

    猜你喜欢
    • 2011-02-22
    • 2016-12-18
    • 1970-01-01
    • 1970-01-01
    • 2013-06-16
    • 2019-09-28
    相关资源
    最近更新 更多