Can I compare a template variable to an integer in Django/App Engine templates?

You are most probably using Django 0.96: The App Engine Python environment includes three versions of Django: 0.96, 1.0.2, and 1.1. Django 0.96 is included with the App Engine SDK, and is the version that gets imported by default when an app imports the django package Source: code.google.com/appengine/docs/python/to... As xyld has said, you must use the ifequal templatetag, because boolean operators were included only in version 1.2, that is currently in beta The documentation for version 0.96 can be found here or you can also use version 1.1: import os os. Environ'DJANGO_SETTINGS_MODULE' = 'settings' from google.appengine. Dist import use_library use_library('django', '1.1') Of course, you can always download the entire Django project, and include it in your application's top level directory.

Some tips on how to do that can be found in this article EDIT : Since the ifequal is not suited for integers, you can pass additional variables to your template class MyHandler(webapp. RequestHandler): def get(self): foo_list = db. GqlQuery(...) ... template_values'foos' = foo_list template_values'foo_count' = len(foo_list) template_values'one_foo' = len(foo_list) == 1 handler.response.out.

Write(template. Render(...)) and in the template: {% if one_foo %} You have one foo. {% endif %} or: {% if foo_list %} You have {{ foo_count }} foo{{foo_count|pluralize}}.

{% else %} You have no foos {% endif %}.

You are most probably using Django 0.96: The App Engine Python environment includes three versions of Django: 0.96, 1.0.2, and 1.1. Django 0.96 is included with the App Engine SDK, and is the version that gets imported by default when an app imports the django package. Source: code.google.com/appengine/docs/python/to... As xyld has said, you must use the ifequal templatetag, because boolean operators were included only in version 1.2, that is currently in beta. The documentation for version 0.96 can be found here or you can also use version 1.1: import os os.

Environ'DJANGO_SETTINGS_MODULE' = 'settings' from google.appengine. Dist import use_library use_library('django', '1.1') Of course, you can always download the entire Django project, and include it in your application's top level directory. Some tips on how to do that can be found in this article.

EDIT: Since the ifequal is not suited for integers, you can pass additional variables to your template. Class MyHandler(webapp. RequestHandler): def get(self): foo_list = db.

GqlQuery(...) ... template_values'foos' = foo_list template_values'foo_count' = len(foo_list) template_values'one_foo' = len(foo_list) == 1 handler.response.out. Write(template. Render(...)) and in the template: {% if one_foo %} You have one foo.

{% endif %} or: {% if foo_list %} You have {{ foo_count }} foo{{foo_count|pluralize}}. {% else %} You have no foos {% endif %}.

From the documentation of ifequal: "It is only possible to compare an argument to template variables or strings. ". Therefore it sounds unsuitable to be used for comparisons to an integer (to test the length of a list) – matt be May 5 '10 at 13:44 Sorry!

I have edited my answer – jbochi May 5 '10 at 14:44.

Django 1.2 allows for == operators in the {% if %} tag. If you need to compare two integers or other items, you can always use {% ifequal a be %}...{% endifequal %} in older versions of Django. My guess is that you are reading the Development docs (django 1.2), but using django 1.1 or 1.1.1 However, if you need to do {{ foo|length }} and compare the output of that template tag + filter, you won't be able to.

You'll need to fix your view logic to calculate that for you so you can use it in the template. Also, depending on how you are using the list, you can look into for...empty: docs.djangoproject.com/en/dev/ref/templa....

Because this seems pretty simple to me - it's not as if I am attempting to execute actual code, just a few if statements. – matt be May 5 '10 at 1:44 if you are simply comparing two ints, its not complex, but older Django's (pre-1.2, which isnt formally released yet) need to use {% ifequal %}, try using ifequal and see if that works instead of {% if 1 == 1 %} – dlamotte May 5 '10 at 1:49 The current development version docs state "It is only possible to compare an argument to template variables or strings." – matt be May 5 '10 at 1:51 1 How you define "too complicated" depends on what you are using the templating language for. In other templating languages this logic is child's play - they give you a lot more expressiveness than Django templates.

Anyway, if you do decide to go with another templating engine, consider Mako if you're comfortable with Python. – David Underhill May 5 '10 at 1:56 True, 'too complicated' is very subjective. The idea behind not letting you do things 'too complicated' was to avoid too much logic in templates.

Not only is it a spot for error, but the initial intent of the templates was for web designers who may or may not have programming experience. Therefore, too much 'complexity' in templates could confuse them... – dlamotte May 5 '10 at 2:08.

You're using a template variable named foo_list: {% if foo_list == 1 %} but there's no such variable in the template your code builds, only foo and foo_count.

AFAIK, undefined template vars are OK and just get ignored. Right? At the very least, its not a syntax error... – dlamotte May 5 '10 at 1:44 sorry, this was a typo caused by me trying to take real code and dumb it down - let me fix that.

– matt be May 5 '10 at 1:45.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions