2011-04-15 39 views
2

我对Google应用引擎非常陌生,刚刚阅读完入门。所以我开始在GAE上实现一个简单的应用程序,很快出现了一个错误,说“TemplateSyntaxError”,“if”语句格式不正确“”,当我简单地呈现一个使用if语句的html。我使用ifequal语句解决了问题,但本教程向我展示了如何在index.html(http://code.google.com/appengine/docs/python/gettingstarted/templates.html)谷歌应用引擎上的Django模板if语句

中使用if语句如果在模板中使用语句,我是否会错过任何内容?

感谢, 柳

更新:
下面是错误的详细信息。我认为克里斯的回答会让我使用“智能”标签。我会尽快将Django版本更新到1.2。

Traceback (most recent call last): 
    File "C:\Program Files\Google\google_appengine\google\appengine\ext\webapp\__init__.py", line 634, in __call__ 
    handler.get(*groups) 
    File "C:\Store house\gae\community\src\community.py", line 24, in get 
    self.response.out.write(template.render(path, template_values)) 
    File "C:\Program Files\Google\google_appengine\google\appengine\ext\webapp\template.py", line 72, in render 
    t = load(template_path, debug) 
    File "C:\Program Files\Google\google_appengine\google\appengine\ext\webapp\template.py", line 100, in load 
    template = django.template.loader.get_template(file_name) 
    File "C:\Program Files\Google\google_appengine\lib\django_0_96\django\template\loader.py", line 80, in get_template 
    template = get_template_from_string(source, origin, template_name) 
    File "C:\Program Files\Google\google_appengine\lib\django_0_96\django\template\loader.py", line 88, in get_template_from_string 
    return Template(source, origin, name) 
    File "C:\Program Files\Google\google_appengine\lib\django_0_96\django\template\__init__.py", line 158, in __init__ 
    self.nodelist = compile_string(template_string, origin) 
    File "C:\Program Files\Google\google_appengine\lib\django_0_96\django\template\__init__.py", line 174, in compile_string 
    return parser.parse() 
    File "C:\Program Files\Google\google_appengine\lib\django_0_96\django\template\__init__.py", line 273, in parse 
    compiled_result = compile_func(self, token) 
    File "C:\Program Files\Google\google_appengine\lib\django_0_96\django\template\loader_tags.py", line 154, in do_extends 
    nodelist = parser.parse() 
    File "C:\Program Files\Google\google_appengine\lib\django_0_96\django\template\__init__.py", line 273, in parse 
    compiled_result = compile_func(self, token) 
    File "C:\Program Files\Google\google_appengine\lib\django_0_96\django\template\loader_tags.py", line 132, in do_block 
    nodelist = parser.parse(('endblock', 'endblock %s' % block_name)) 
    File "C:\Program Files\Google\google_appengine\lib\django_0_96\django\template\__init__.py", line 273, in parse 
    compiled_result = compile_func(self, token) 
    File "C:\Program Files\Google\google_appengine\lib\django_0_96\django\template\defaulttags.py", line 655, in do_if 
    raise TemplateSyntaxError, "'if' statement improperly formatted" 
TemplateSyntaxError: 'if' statement improperly formatted 

更新2:
根据这篇文章(http://code.google.com/appengine/docs/python/tools/libraries.html#Django),谷歌当前应用程序引擎已经包含的Django 1.2但当前的默认版本是0.96,这就是为什么我不能使用标签。要使用1.2版本,请按照上面链接中的说明操作。现在,我可以使用“智能”标签。谢谢大家:)

+2

你能告诉您的模板,引发错误的摘录? – arie 2011-04-15 13:40:23

回答

5

如果你试图使用标签:

{% if x == 1 %} 

和你得到的是与

{% ifequal x 1 %} 

解决的一个错误是一个迹象,你的Django的版本是1.1或更低。 “智能”如果标签came with version 1.2

编辑添加,Django 1.2和更高版本在GAE上工作。如果您确实正在运行旧版本的Django,请参阅this blog post了解如何设置它。

+0

查看我的更新2.谷歌应用程序引擎已经包含1.2版本,但是,目前您必须声明您要使用的版本,因为默认版本是0.96。无论如何,你的回答帮助我弄清楚发生了什么。谢谢! – 2011-04-16 03:45:07

0

确保您完全遵循django模板的语法。确保你有你的密码{% %}。还确保您有{%,%}for之间的空格。 要知道更多的只是通过这个Django templating

{% if var1 %} 
{{ var1|safe }} 
{% endif %} 
+1

空格是可选的。 – 2011-04-15 14:10:29

+1

endif for the closing statement this wont work。 – topless 2011-04-15 20:35:29

+0

这将引发模板语法异常。 – zerofuxor 2011-04-27 16:07:44