Google App Engine (python): TemplateSyntaxError: &

2019-02-17 06:26发布

This is using the web app framework, not Django.

The following template code is giving me an TemplateSyntaxError: 'for' statements with five words should end in 'reversed' error when I try to render a dictionary. I don't understand what's causing this error. Could somebody shed some light on it for me?

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

I'm rendering it using the following:

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))

1条回答
再贱就再见
2楼-- · 2019-02-17 07:19

This is using the web app framework, not Django.

But framework apart, you must be using Django's templating -- and apparently in an old version, which does not support the "automatic unpacking" style of for -- probably the 0.96 version that's the default for App Engine. To use any part of more modern Django (including "just the templates") you must have a settings.py file and do:

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

from google.appengine.dist import use_library
use_library('django', '1.1')

as per the docs. After that you can from django import template and you'll be using the 1.1 version of Django's templating engine.

查看更多
登录 后发表回答