2
votes

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 variable

3

3 Answers

2
votes

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')
2
votes

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.

0
votes

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 %}