python - tornado not redirecting to new dummy page

馋奶兔 提交于 2019-12-11 14:14:16

问题


I am using tornado and here is the code for my main handler -

class MainHandler(tornado.web.RequestHandler):

    def get(self):

        self.render("homepage.html", messages=ChatSocketHandler.browser_cache)

#
# HTTP *POST* HANDLER
#
    def post(self):
        print "Post received"
        post_body = self.get_argument("body")
        self.set_header("Content-Type", "text/plain")
        self.redirect("test.html")

The test.html file is simple -

<!DOCTYPE html>
  <html>
    <body>

     <h1>My First Heading</h1>

     <p>My first paragraph.</p>

     </body>
   </html>

I put it both, in the same folder as the python server file and also in the templates directory. Yet, when the post handler is called, the page does not redirect to this file instead giving me a 404.

[W 151101 22:35:44 web:1825] 404 GET /test.html (::1) 1.00ms

What am I missing?


回答1:


Just putting an html file in the same directory as your python file doesn't do anything in Tornado: you must tell the python server to serve this file. In general, for every top-level file in your templates directory you should create a handler that calls self.render() for that template, and create a route for that file in your application definition.

If you don't want to use any template features in this file, you could put it in your static_path instead of template_path, and then it would be served automatically as /static/index.html. But it sounds like you probably want this file to evolve towards being a more full-featured handler.



来源:https://stackoverflow.com/questions/33472847/python-tornado-not-redirecting-to-new-dummy-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!