我正试图通过Tornado在2个HTML页面之间导航。以下是路由及其各自处理程序的代码:
class MainHandler(tornado.web.RequestHandler):
def get(self):
log.info("Rendering index.html")
self.render("index.html")
class NotificationsPageHandler(tornado.web.RequestHandler):
def get(self):
log.info("Rendering notifications")
self.render("notifications.html")
def start_server():
settings = {
"static_path": os.path.join(os.path.dirname(__file__), "static")
}
application = tornado.web.Application([
(r"/", MainHandler),
(r"/notifications.html", NotificationsPageHandler),
], **settings)
application.listen(8989)
tornado.ioloop.IOLoop.current().start()
当我在浏览器上加载127.0.0.1:8989时,我得到index.html页面但是当我尝试通过index.html中的锚标记导航到notifications.html时,我得到以下堆栈跟踪:
2016-07-06 12:07:06,546 - tornado.application - ERROR - Uncaught exception GET /notifications.html (127.0.0.1)
HTTPServerRequest(protocol='http', host='127.0.0.1:8989', method='GET', uri='/notifications.html', version='HTTP/1.1', remote_ip='127.0.0.1', headers={'Accept-Language': 'en-US,en;q=0.8', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Host': '127.0.0.1:8989', 'Upgrade-Insecure-Requests': '1', 'Accept-Encoding': 'gzip, deflate, sdch', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36', 'Referer': 'http://127.0.0.1:8989/', 'Connection': 'keep-alive'})
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/tornado/web.py", line 1443, in _execute
result = method(*self.path_args, **self.path_kwargs)
File "BADWebServer.py", line 231, in get
self.render("notifications.html")
File "/usr/local/lib/python3.5/dist-packages/tornado/web.py", line 699, in render
html = self.render_string(template_name, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/tornado/web.py", line 806, in render_string
return t.generate(**namespace)
File "/usr/local/lib/python3.5/dist-packages/tornado/template.py", line 345, in generate
return execute()
File "notifications_html.generated.py", line 5, in _tt_execute
_tt_tmp = item.score # notifications.html:37
NameError: name 'item' is not defined
2016-07-06 12:07:06,548 - tornado.access - ERROR - 500 GET /notifications.html (127.0.0.1) 4.51ms
我见过类似的帖子how to navigate from one html to other in tornado using anchor tag,但我不知道为什么我会得到例外。
答案 0 :(得分:0)
您收到错误的原因是,正如跟踪所说," name' item'未定义"。您的notifications.html模板包含一些标记,如:
{{ item.score }}
...但你还没有通过"项目"变量。See the template syntax guide for an example。