在 Django 模板中构建列表 [英] Building a list in Django templates

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

问题描述

使用此代码:

{% for o in [1,2,3] %}<div class="{% cycle 'row1' 'row2' %}">{% 循环 'row1' 'row2' %}

{% 结束为 %}

我得到一个TemplateSyntaxError:

无法解析余数:'[1,2,3]' from '[1,2,3]'

有没有办法在模板中构建列表?

解决方案

您可以巧妙地使用 make_list 过滤器,但这可能是个坏主意:

{% for o in "123"|make_list %}<div class="{% cycle 'row1' 'row2' %}">{% 循环 'row1' 'row2' %}

{% 结束为 %}

ps.您似乎没有在任何地方使用 o,所以我不确定您要做什么.

With this code:

{% for o in [1,2,3] %}
    <div class="{% cycle 'row1' 'row2' %}">
        {% cycle 'row1' 'row2' %}
    </div>
{% endfor %}

I get a TemplateSyntaxError:

Could not parse the remainder: '[1,2,3]' from '[1,2,3]'

Is there a way of building a list in a template?

解决方案

You can do it via cunning use of the make_list filter, but it's probably a bad idea:

{% for o in "123"|make_list %}
    <div class="{% cycle 'row1' 'row2' %}">
        {% cycle 'row1' 'row2' %}
    </div>
{% endfor %}

p.s. You don't seem to be using o anywhere, so I'm not sure what you're trying to do.

这篇关于在 Django 模板中构建列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆