I'm in process of learning how Tornado's Modules and Templates work. In this particular example http://localhost:8000/
returns a proper page, but http://localhost:8000/recommended
returns 404 and following description:
Traceback (most recent call last):
File "/home/stefan/.local/lib/python3.6/site-packages/tornado/web.py", line 1676, in _execute
result = self.prepare()
File "/home/stefan/.local/lib/python3.6/site-packages/tornado/web.py", line 2431, in prepare
raise HTTPError(self._status_code)
tornado.web.HTTPError: HTTP 404: Not Found
This is my main.py
import os.path
import tornado.web
import tornado.httpserver
import tornado.ioloop
import tornado.options
from tornado.options import define, options
define("port", default=8000, help="run on given port", type=int)
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", MainHandler),
(r"/recommended/", RecommendedHandler),
]
settings = dict(
template_path = os.path.join(os.path.dirname(__file__),"templates"),
static_path = os.path.join(os.path.dirname(__file__),"static"),
ui_modules = {"Book" : BookModule },
debug=True,
)
tornado.web.Application.__init__(self,handlers,**settings)
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render("index.html", page_title="Burt's Books | Home ", header_text = "Welcome to Burt's Books!",)
class RecommendedHandler(tornado.web.RequestHandler):
def get(self):
self.render(
"recommended.html",
page_title = "Burt's Books | recommended Reading",
header_text = "Recommended Reading",
books = [
{
"title" : "Programming Collective Intelligence",
},
...
]
)
class BookModule(tornado.web.UIModule):
def render(self,book):
return self.render_string("modules/book.html", book=book)
if __name__ == "__main__":
tornado.options.parse_command_line()
http_server = tornado.httpserver.HTTPServer(Application())
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
Structure of my working directory is:
Burt's Books/
/static
/templates
/modules
book.html
index.html
main.html
recommeneded.html
main.py
I can post content of other files in comment, if needed.
It looks like you are requesting /recommended
but your mapping is /recommended/
. I always add a question-mark at the end of my path structures to ensure that the regular expression covers both. Try changing the code to the following:
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", MainHandler),
(r"/recommended/?", RecommendedHandler),
]
...