【问题标题】:Could not parse the remainder: '%' from '%' Django无法解析剩余部分:来自 '%' Django 的 '%'
【发布时间】:2015-12-31 07:11:06
【问题描述】:

我正在与DjangoMezannine 合作,我遇到了一个关于 jinja 的奇怪问题。

TemplateSyntaxError at /services/
Could not parse the remainder: '%' from '%'
Request Method: GET
Request URL:    http://192.168.1.14/services/
Django Version: 1.8.3
Exception Type: TemplateSyntaxError
Exception Value:    
Could not parse the remainder: '%' from '%'
Exception Location: /usr/local/lib/python3.4/dist-packages/django/template/base.py in __init__, line 639
Python Executable:  /usr/bin/python3
Python Version: 3.4.3

我的代码如下所示:

{% for image in images %}
   {% if loop.index % 3 == 0 %} #this is the line it doesn't like
     {{image}}
    {% endif %}
{% endfor %}

知道这里发生了什么吗?

谢谢

【问题讨论】:

    标签: python django jinja2


    【解决方案1】:

    % 是 Django 保留的,所以你必须使用 divisibleby

    {% for image in images %}
        {% if forloop.counter|divisibleby:"3" %}
           {{image}}
        {% endif %}
    {% endfor %}
    

    【讨论】:

    • "3" 还是 3?不需要是数字吗?
    • 复制并粘贴您的行。新错误:int() 参数必须是字符串、类似字节的对象或数字,而不是 'NoneType'
    • 搞清楚了;它实际上是 {% if forloop.counter|divisibleby:"3" %}
    • 它是 .counter 而不是 .index
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 2017-11-18
    • 1970-01-01
    • 2013-11-22
    • 2016-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多