我有一个简单的Jinja模板测试片段,我正在编写HTML模板,但出于某种原因,循环崩溃了在线应用引擎和devappserver。我尝试用常量替换我的for循环的主题只是为了测试实际的循环,并根据Cloud SDK Shell错误消息我将它隔离到for循环的实际语法。这个片段有什么问题?
错误:
File "C:\Users\Skyler\Documents\Reverie\reflect\templates\index.html",
line 9,
in template
{{% for test in my_list %}}
TemplateSyntaxError: unexpected '%'
的index.html
<!DOCTYPE html>
<html>
<head>
<title>{{title}}</title>
</head>
<body>
<h1> {{my_list[3]}} </h1>
<ul>
{{% for test in my_list %}}
<li>{{my_list[1]}}</li>
{{% endfor %}}
</ul>
</body>
</html>
test.py
import webapp2
import jinja2
import os
template_dir = os.path.join(os.path.dirname(__file__), "templates")
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir), autoescape = True)
class Handler(webapp2.RequestHandler):
def write(self, *a, **kw):
self.response.write(*a, **kw)
def render_str(self, template, **params):
t = jinja_env.get_template(template)
return t.render(params)
def render(self, template, **kw):
self.write(self.render_str(template, **kw))
class MainPage(Handler):
"""docstring for MainPage"""
def get(self):
self.render("index.html", title="Nani", my_list=[0,1,2,"string"])
app = webapp2.WSGIApplication([('/', MainPage),], debug=True)
的app.yaml
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /
script: test.app
libraries:
- name: jinja2
version: latest
完整Stacktrace
ERROR 2017-07-18 05:02:36,960 webapp2.py:1528] unexpected '%'
Traceback (most recent call last):
File "C:\Users\Skyler\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform
\google_appengine\lib\webapp2-2.3\webapp2.py", line 1511, in __call__
rv = self.handle_exception(request, response, e)
File "C:\Users\Skyler\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform
\google_appengine\lib\webapp2-2.3\webapp2.py", line 1505, in __call__
rv = self.router.dispatch(request, response)
File "C:\Users\Skyler\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform
\google_appengine\lib\webapp2-2.3\webapp2.py", line 1253, in default_dispatcher
return route.handler_adapter(request, response)
File "C:\Users\Skyler\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform
\google_appengine\lib\webapp2-2.3\webapp2.py", line 1077, in __call__
return handler.dispatch()
File "C:\Users\Skyler\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform
\google_appengine\lib\webapp2-2.3\webapp2.py", line 547, in dispatch
return self.handle_exception(e, self.app.debug)
File "C:\Users\Skyler\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform
\google_appengine\lib\webapp2-2.3\webapp2.py", line 545, in dispatch
return method(*args, **kwargs)
File "C:\Users\Skyler\Documents\Reverie\reflect\test.py", line 22, in get
self.render("index.html", title="Nani", my_list=[0,1,2,"string"])
File "C:\Users\Skyler\Documents\Reverie\reflect\test.py", line 17, in render
self.write(self.render_str(template, **kw))
File "C:\Users\Skyler\Documents\Reverie\reflect\test.py", line 13, in render_s
tr
t = jinja_env.get_template(template)
File "C:\Users\Skyler\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform
\google_appengine\lib\jinja2-2.6\jinja2\environment.py", line 719, in get_templa
te
return self._load_template(name, self.make_globals(globals))
File "C:\Users\Skyler\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform
\google_appengine\lib\jinja2-2.6\jinja2\environment.py", line 693, in _load_temp
late
template = self.loader.load(self, name, globals)
File "C:\Users\Skyler\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform
\google_appengine\lib\jinja2-2.6\jinja2\loaders.py", line 127, in load
code = environment.compile(source, name, filename)
File "C:\Users\Skyler\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform
\google_appengine\lib\jinja2-2.6\jinja2\environment.py", line 493, in compile
self.handle_exception(exc_info, source_hint=source)
File "C:\Users\Skyler\Documents\Reverie\reflect\templates\index.html", line 9,
in template
{{% for test in my_list %}}
TemplateSyntaxError: unexpected '%'
ERROR 2017-07-18 05:02:36,960 wsgi.py:279]
Traceback (most recent call last):
File "C:\Users\Skyler\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform
\google_appengine\google\appengine\runtime\wsgi.py", line 267, in Handle
result = handler(dict(self._environ), self._StartResponse)
File "C:\Users\Skyler\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform
\google_appengine\lib\webapp2-2.3\webapp2.py", line 1519, in __call__
response = self._internal_error(e)
File "C:\Users\Skyler\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform
\google_appengine\lib\webapp2-2.3\webapp2.py", line 1511, in __call__
rv = self.handle_exception(request, response, e)
File "C:\Users\Skyler\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform
\google_appengine\lib\webapp2-2.3\webapp2.py", line 1505, in __call__
rv = self.router.dispatch(request, response)
File "C:\Users\Skyler\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform
\google_appengine\lib\webapp2-2.3\webapp2.py", line 1253, in default_dispatcher
return route.handler_adapter(request, response)
File "C:\Users\Skyler\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform
\google_appengine\lib\webapp2-2.3\webapp2.py", line 1077, in __call__
return handler.dispatch()
File "C:\Users\Skyler\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform
\google_appengine\lib\webapp2-2.3\webapp2.py", line 547, in dispatch
return self.handle_exception(e, self.app.debug)
File "C:\Users\Skyler\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform
\google_appengine\lib\webapp2-2.3\webapp2.py", line 545, in dispatch
return method(*args, **kwargs)
File "C:\Users\Skyler\Documents\Reverie\reflect\test.py", line 22, in get
self.render("index.html", title="Nani", my_list=[0,1,2,"string"])
File "C:\Users\Skyler\Documents\Reverie\reflect\test.py", line 17, in render
self.write(self.render_str(template, **kw))
File "C:\Users\Skyler\Documents\Reverie\reflect\test.py", line 13, in render_s
tr
t = jinja_env.get_template(template)
File "C:\Users\Skyler\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform
\google_appengine\lib\jinja2-2.6\jinja2\environment.py", line 719, in get_templa
te
return self._load_template(name, self.make_globals(globals))
File "C:\Users\Skyler\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform
\google_appengine\lib\jinja2-2.6\jinja2\environment.py", line 693, in _load_temp
late
template = self.loader.load(self, name, globals)
File "C:\Users\Skyler\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform
\google_appengine\lib\jinja2-2.6\jinja2\loaders.py", line 127, in load
code = environment.compile(source, name, filename)
File "C:\Users\Skyler\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform
\google_appengine\lib\jinja2-2.6\jinja2\environment.py", line 493, in compile
self.handle_exception(exc_info, source_hint=source)
File "C:\Users\Skyler\Documents\Reverie\reflect\templates\index.html", line 9,
in template
{{% for test in my_list %}}
TemplateSyntaxError: unexpected '%'
INFO 2017-07-17 22:02:36,970 module.py:832] default: "GET / HTTP/1.1" 500 -
答案 0 :(得分:1)
可能是因为你似乎过多使用一个花括号。查看docs:
中的for循环 <title>{% block title %}{% endblock %}</title>
<ul>
{% for user in users %}
<li><a href="{{ user.url }}">{{ user.username }}</a></li>
{% endfor %}
</ul>
语句{% for user in users %}
中只有一个大括号,而你的语句使用两个大括号:{{% for test in my_list %}}
。
请改为:{% for test in my_list %}
并查看会发生什么。