【问题标题】:Invalid syntax jinja2 template value无效的语法 jinja2 模板值
【发布时间】:2013-03-21 01:49:51
【问题描述】:

为什么 ma​​x_height 会产生无效的语法错误?

ma​​in.py

max_height = 70

template_values = {   
   'max_height': max_height    # syntax error
   ...
}

index.html

<html>
    <body>      
        {% for person in people %}
            {% if person.filter("height <", max_height %)
                <b>{{ person.first_name }}</b> 
                <b>{{ person.last_name }}</b>
                <b>{{ person.city }}</b> 
                <b>{{ person.birth_year }}</b> 
                <b>{{ person.height }}</b> 
                <hr></hr>
            {% endif %}
        {% endfor %}
    </body>
</html>

编辑 1 这是 ma​​in.py 中的 MainPage 类:

class MainPage(webapp2.RequestHandler):
    def get(self):

        people_query = Person.all()
        people = people_query.fetch(10)

        max_height = 70

        template_values = {
            'people': people
            'max_height': max_height
        }

        template = jinja_environment.get_template('index.html')
        self.response.out.write(template.render(template_values))

【问题讨论】:

    标签: google-app-engine python-2.7 jinja2


    【解决方案1】:

    这一行:

    {% if person.filter("height <", max_height %)
    

    应该是这样的:

    {% if person.filter("height <", max_height) %}
    

    另外,我建议不要在模板本身中使用任何类型的过滤逻辑。将该代码放入您的应用程序代码中,然后使用模板来呈现 HTML。

    【讨论】:

    • +1 谢谢搅拌机。我修复了代码。我仍然遇到同样的错误:'max_height': max_height.是不是模板值不允许我这样传递变量?
    • 错误是...第 20 行'max_height':max_height。 SyntaxError: 无效语法
    • @Anthony:尝试在max_height 后面加一个逗号。
    猜你喜欢
    • 2020-05-27
    • 2013-05-12
    • 2017-03-29
    • 2019-11-05
    • 1970-01-01
    • 2014-02-11
    • 1970-01-01
    • 2014-04-07
    相关资源
    最近更新 更多