【问题标题】:[Python][Tornado]: 500 Internal Server Error when navigating between pages[Python][Tornado]:在页面之间导航时出现 500 内部服务器错误
【发布时间】:2016-07-06 19:13:29
【问题描述】:

我正在尝试通过 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 中的锚标记导航到 notification.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,但我不确定为什么会出现异常。

【问题讨论】:

    标签: html python-3.x tornado


    【解决方案1】:

    您收到错误是因为,正如跟踪所说,“名称'item'未定义”。您的 notifications.html 模板包含一些标记,例如:

    {{ item.score }}
    

    ...但你还没有传入一个“item”变量。See the template syntax guide for an example

    【讨论】:

    • 是的,这是真的。但我期待 Tornado 网络服务器只是将 HTML 返回给客户端,而不是通过它进行解析。我想我对 Web 服务器如何工作的理解是错误的!从您共享的链接来看,Tornado 似乎也可以对 HTML 进行一些计算。 HTML 文件有 {{ item.score }} 用于客户端的 Angular。有什么建议可以防止 Tornado 解析 HTML 并盲目返回它吗?
    • 如果不想使用 Tornado 模板渲染,可以用 StaticFileHandler 代替 RequestHandler 来返回内容。
    猜你喜欢
    • 1970-01-01
    • 2011-07-14
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 2016-04-25
    • 1970-01-01
    • 2011-05-05
    • 1970-01-01
    相关资源
    最近更新 更多