Syed Safir Syed Safir - 1 year ago 226
HTML Question

[Python][Tornado]: 500 Internal Server Error when navigating between pages

I'm trying to navigate between 2 HTML pages through Tornado. Following is the code for the routes and their respective handlers:

class MainHandler(tornado.web.RequestHandler):
def get(self):"Rendering index.html")

class NotificationsPageHandler(tornado.web.RequestHandler):
def get(self):"Rendering notifications")

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)


When I load on the browser, I get the index.html page but when I try to navigate to notifications.html through an anchor tag in index.html, I get the following stack trace:

2016-07-06 12:07:06,546 - tornado.application - ERROR - Uncaught exception GET /notifications.html (
HTTPServerRequest(protocol='http', host='', method='GET', uri='/notifications.html', version='HTTP/1.1', remote_ip='', 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': '', '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': '', 'Connection': 'keep-alive'})
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/tornado/", line 1443, in _execute
result = method(*self.path_args, **self.path_kwargs)
File "", line 231, in get
File "/usr/local/lib/python3.5/dist-packages/tornado/", line 699, in render
html = self.render_string(template_name, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/tornado/", line 806, in render_string
return t.generate(**namespace)
File "/usr/local/lib/python3.5/dist-packages/tornado/", line 345, in generate
return execute()
File "", 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 ( 4.51ms

I have seen a similar post, how to navigate from one html to other in tornado using anchor tag but I'm not sure why I'm getting the exception.

Answer Source

You're getting the error because, as the trace says, "name 'item' is not defined". Your notifications.html template contains some markup like:

{{ item.score }}

... but you haven't passed an "item" variable in. See the template syntax guide for an example.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download