2016-07-06 81 views
0

我想通過Tornado瀏覽2個HTML頁面。以下是路由和他們各自處理的代碼:[Python] [龍捲風]:500頁面間導航時出現內部服務器錯誤

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頁面,但是當我嘗試導航通過對notifications.html index.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

您會收到錯誤消息,因爲跟蹤顯示「名稱」項目未定義。你notifications.html模板包含一些標記,如:

{{ item.score }} 

...但你沒有通過在See the template syntax guide for an example「項目」變量。

+0

是的,的確如此。但我期待着Tornado web服務器將HTML回饋給客戶端,而不是通過它解析。我想我對Web服務器如何工作的理解是錯誤的!從你共享的鏈接看來,Tornado似乎也可以對HTML做一些計算。 HTML文件的{{item.score}}用於客戶端的Angular。任何建議如何防止Tornado解析HTML並一味返回? –

+0

如果您不想使用Tornado模板渲染,請使用StaticFileHandler而不是RequestHandler返回內容。 –