开发者

GAE/Django TemplateError

I have a google app engine app that is using Django templates. I am trying to do this:

{% if person.age >= 4 %}
      <p style="color:White">Hello</p>
{% else %}
      <p style="color:Black">Hello</p>
{% endif %}

This doesn't work, I get: "TemplateSyntaxError: 'if' statement improperly formatted". Meanwhile (because some values are None) this does work:

{% if person.age %}
      <p style="color:White">Hello</p>
{% else %}
      <p style="color:Black">Hello</p>
{% endif %}

I dont see why the first example doesnt work, here is the link to Django Templates I think I am following the operator examples close enough other than the fact that this is a property and not a variab开发者_如何学Gole


What Django version are you using?

The default version on App Engine is 0.96, and the >= operator was not supported until 1.2.

You can switch to 1.2 by adding this above any of your template-related imports:

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

from google.appengine.dist import use_library
use_library('django', '1.2')


Do if person.age and person.age >= 4

That way, if person.age is None, it will fail the first check and not throw the syntax error on the second one.


As you are saying, as some values are none, it will throw syntax error. Better you can check for the existence of person.age and then do the check like this.

{% if person.age and person.age >= 4 %}
      <p style="color:White">Hello</p>
{% else %}
      <p style="color:Black">Hello</p>
{% endif %}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜