【问题标题】:Django TemplateSyntaxError: Could not parse the remainder: '"{%' from '"{%'Django TemplateSyntaxError: 无法解析余数:'"{%' from '"{%'
【发布时间】:2020-06-20 13:50:15
【问题描述】:

我试图在 for 循环中执行循环,但它会引发错误。什么是正确的选择。

{% cycle "{% include 'col-1-of-3.html' %}" "{% include 'col-2-of-3.html' %}" "{% include 'col-3-of-3.html' %}" %}

错误:

TemplateSyntaxError: Could not parse the remainder: '"{%' from '"{%'

【问题讨论】:

    标签: django django-templates django-template-filters


    【解决方案1】:

    只需使用要包含在{% cycle … %} template tag [Django-doc] 中的文件名,并将其分配给变量(例如tmp)。然后你可以在{% include … %} template tag [Django-doc]中使用那个变量:

    {% for variable in loop %}
        {% cycle 'col-1-of-3.html' 'col-2-of-3.html' 'col-3-of-3.html' as tmp silent %}
        {% include tmp %}
    {% endfor %}

    【讨论】:

    • TemplateDoesNotExist at /tools-dash/ 收到此错误。三个 html 文件位于模板/工具文件夹中
    • @VAsplash:您当然需要指定模板的正确路径。 Django 不会查看同一目录中的文件。如果你的目录是tools,它应该是tools/col-1-of-3.html(当然其他文件也一样)。
    • 它有效,但是对于每个循环,它都会在每个循环上显示 tools/col-1-of-3.html 和 html 中的其他内容
    • @VAsplash: 嗯,是的,你需要在{% cycle %}的末尾加上silent
    猜你喜欢
    • 2013-10-04
    • 2023-02-08
    • 2018-08-31
    • 2011-07-11
    • 2015-03-06
    • 2011-11-06
    • 2018-03-31
    • 2015-03-17
    • 1970-01-01
    相关资源
    最近更新 更多