在呈现的页面上获取 404
Getting 404 on rendered page
我正在学习 Tornado 的模块和模板是如何工作的。在这个特定的例子中 http://localhost:8000/
returns 一个正确的页面,但是 http://localhost:8000/recommended
returns 404 和以下描述:
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
这是我的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()
我的工作目录结构是:
伯特的书/
/static
/templates
/modules
book.html
index.html
main.html
recommeneded.html
main.py
如果需要,我可以post评论其他文件的内容。
您似乎在请求 /recommended
,但您的映射是 /recommended/
。我总是在路径结构的末尾添加一个问号,以确保正则表达式涵盖两者。尝试将代码更改为以下内容:
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", MainHandler),
(r"/recommended/?", RecommendedHandler),
]
...
我正在学习 Tornado 的模块和模板是如何工作的。在这个特定的例子中 http://localhost:8000/
returns 一个正确的页面,但是 http://localhost:8000/recommended
returns 404 和以下描述:
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
这是我的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()
我的工作目录结构是:
伯特的书/
/static
/templates
/modules
book.html
index.html
main.html
recommeneded.html
main.py
如果需要,我可以post评论其他文件的内容。
您似乎在请求 /recommended
,但您的映射是 /recommended/
。我总是在路径结构的末尾添加一个问号,以确保正则表达式涵盖两者。尝试将代码更改为以下内容:
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", MainHandler),
(r"/recommended/?", RecommendedHandler),
]
...