【问题标题】:Google App Engine (python): TemplateSyntaxError: 'for' statements with five words should end in 'reversed'Google App Engine (python): TemplateSyntaxError: 'for' statements with 5 words should end in 'reversed'
【发布时间】:2011-02-03 12:52:49
【问题描述】:

这是使用网络应用程序框架,而不是 Django。

当我尝试渲染字典时,以下模板代码给了我一个 TemplateSyntaxError: 'for' statements with 'reversed' 错误。我不明白是什么导致了这个错误。有人可以帮我解释一下吗?

{% for code, name in charts.items %}
   <option value="{{code}}">{{name}}</option>
{% endfor %}

我正在使用以下内容进行渲染:

class GenerateChart(basewebview):

    def get(self):
        values = {"datepicker":True}
        values["charts"] = {"p3": "3D Pie Chart", "p": "Segmented Pied Chart"}
        self.render_page("generatechart.html", values)

class basewebview(webapp.RequestHandler):
    ''' Base class for all webapp.RequestHandler type classes '''
    def render_page(self, filename, template_values=dict()):
        filename = "%s/%s" % (_template_dir, filename)
        path = os.path.join(os.path.dirname(__file__), filename)
        self.response.out.write(template.render(path, template_values))

【问题讨论】:

    标签: python django google-app-engine django-templates


    【解决方案1】:

    这是使用网络应用程序框架, 不是 Django。

    但是除了框架,你必须使用 Django 的模板——而且显然是在一个旧版本中,它不支持 for 的“自动解包”样式——可能是 App Engine 的默认 0.96 版本.要使用更现代的 Django 的任何部分(包括“仅模板”),您必须拥有一个 settings.py 文件并执行以下操作:

    import os
    os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
    
    from google.appengine.dist import use_library
    use_library('django', '1.1')
    

    根据the docs。之后您可以from django import template 使用 Django 模板引擎的 1.1 版本。

    【讨论】:

    • Martellipedia 又来了!谢谢。
    猜你喜欢
    • 2014-01-22
    • 2012-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2021-11-29
    相关资源
    最近更新 更多