2

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 Answers 3

2

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') 
Sign up to request clarification or add additional context in comments.

1 Comment

I guess technically I am using Google's webapp framework which, as I understand it, uses Django templates. I am using Django templates all over the place and they work fine. This is the first place I am using an operator though. Your soultion of adding the library import got it. Thanks.
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.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.