2013-03-21 69 views
-1

为什么MAX_HEIGHT产生一个无效的语法错误?无效的语法Jinja2的模板值

main.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> 

EDIT 1 下面是从main.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)) 

回答

1

这条线:

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

应该是这个样子:

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

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

+0

+1谢谢搅拌机。我修复了代码。我仍然遇到同样的错误:'max_height':max_height。是不是模板值不允许我传递这样的变量? – Anthony 2013-03-21 02:02:39

+1

@Anthony:什么是错误? – Blender 2013-03-21 02:04:26

+0

错误是...第20行'max_height':max_height。 SyntaxError:无效的语法 – Anthony 2013-03-21 02:07:01