简体   繁体   English

Python 龙卷风的自定义 404 错误页面

[英]Custom 404 Errorpage with Python tornado

I'm trying to implement a custom 404 page with python tornado.我正在尝试使用 python 龙卷风实现自定义 404 页面。 The docs say: " default_handler_class and default_handler_args : This handler will be used if no other match is found; use this to implement custom 404 pages (new in Tornado 3.2).".文档说:“ default_handler_classdefault_handler_args :如果没有找到其他匹配项,将使用此处理程序;使用它来实现自定义 404 页面(Tornado 3.2 中的新功能)。”。 There is no good information about how the handler needs to work.没有关于处理程序需要如何工作的好信息。 I tried using this code:我尝试使用此代码:

    class defaultHandler(tornado.web.RequestHandler):
        def __init__(self, arg2, arg3):
            print("Called default handler")
            self.arg2 = arg2
            self.arg3 = arg3
    
        def get(self):
            self.write("404 - my thing")

Using this tornado config:使用这个龙卷风配置:

 return tornado.web.Application([
        (r"/main", MainHandler),
        (r"/update_kurse", updateCallback),
        (r"/", LoginPage),
    ], static_path=os.path.join(os.path.dirname(__file__), "static"),
    template_path=os.path.join(os.path.dirname(__file__), "templates"),
    default_handler_class=defaultHandler)

This results in an error:这会导致错误:

ERROR:tornado.application:Uncaught exception
Traceback (most recent call last):
  File "C:\Users\******\lib\site-packages\tornado\http1connection.py", line 273, in _read_message
    delegate.finish()
  File "C:\Users\******\lib\site-packages\tornado\routing.py", line 268, in finish
    self.delegate.finish()
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 2297, in finish
    self.execute()
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 2337, in execute
    return self.handler._prepared_future
AttributeError: 'defaultHandler' object has no attribute '_prepared_future'
ERROR:tornado.application:Error in exception logger
Traceback (most recent call last):
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 1665, in _execute
    if self.request.method not in self.SUPPORTED_METHODS:
AttributeError: 'function' object has no attribute 'method'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 1752, in _handle_request_exception
    self.log_exception(*sys.exc_info())
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 1790, in log_exception
    self._request_summary(),
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 1740, in _request_summary
    self.request.method,
AttributeError: 'function' object has no attribute 'method'
ERROR:tornado.application:Exception in exception handler
Traceback (most recent call last):
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 1665, in _execute
    if self.request.method not in self.SUPPORTED_METHODS:
AttributeError: 'function' object has no attribute 'method'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 1708, in _execute
    self._handle_request_exception(e)
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 1757, in _handle_request_exception
    if self._finished:
AttributeError: 'defaultHandler' object has no attribute '_finished'
ERROR:asyncio:Exception in callback _HandlerDelegate.execute.<locals>.<lambda>(<Task finishe...ed_future'",)>) at C:\Users\******\lib\site-packages\tornado\web.py:2333
handle: <Handle _HandlerDelegate.execute.<locals>.<lambda>(<Task finishe...ed_future'",)>) at C:\Users\******\lib\site-packages\tornado\web.py:2333>
Traceback (most recent call last):
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 1665, in _execute
    if self.request.method not in self.SUPPORTED_METHODS:
AttributeError: 'function' object has no attribute 'method'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\******\lib\asyncio\events.py", line 145, in _run
    self._callback(*self._args)
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 2333, in <lambda>
    fut.add_done_callback(lambda f: f.result())
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 1714, in _execute
    if self._prepared_future is not None and not self._prepared_future.done():
AttributeError: 'defaultHandler' object has no attribute '_prepared_future'
Called default handler
ERROR:tornado.application:Uncaught exception
Traceback (most recent call last):
  File "C:\Users\******\lib\site-packages\tornado\http1connection.py", line 273, in _read_message
    delegate.finish()
  File "C:\Users\******\lib\site-packages\tornado\routing.py", line 268, in finish
    self.delegate.finish()
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 2297, in finish
    self.execute()
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 2337, in execute
    return self.handler._prepared_future
AttributeError: 'defaultHandler' object has no attribute '_prepared_future'
ERROR:tornado.application:Error in exception logger
Traceback (most recent call last):
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 1665, in _execute
    if self.request.method not in self.SUPPORTED_METHODS:
AttributeError: 'function' object has no attribute 'method'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 1752, in _handle_request_exception
    self.log_exception(*sys.exc_info())
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 1790, in log_exception
    self._request_summary(),
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 1740, in _request_summary
    self.request.method,
AttributeError: 'function' object has no attribute 'method'
ERROR:tornado.application:Exception in exception handler
Traceback (most recent call last):
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 1665, in _execute
    if self.request.method not in self.SUPPORTED_METHODS:
AttributeError: 'function' object has no attribute 'method'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 1708, in _execute
    self._handle_request_exception(e)
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 1757, in _handle_request_exception
    if self._finished:
AttributeError: 'defaultHandler' object has no attribute '_finished'
ERROR:asyncio:Exception in callback _HandlerDelegate.execute.<locals>.<lambda>(<Task finishe...ed_future'",)>) at C:\Users\******\lib\site-packages\tornado\web.py:2333
handle: <Handle _HandlerDelegate.execute.<locals>.<lambda>(<Task finishe...ed_future'",)>) at C:\Users\******\lib\site-packages\tornado\web.py:2333>
Traceback (most recent call last):
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 1665, in _execute
    if self.request.method not in self.SUPPORTED_METHODS:
AttributeError: 'function' object has no attribute 'method'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\******\lib\asyncio\events.py", line 145, in _run
    self._callback(*self._args)
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 2333, in <lambda>
    fut.add_done_callback(lambda f: f.result())
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 1714, in _execute
    if self._prepared_future is not None and not self._prepared_future.done():
AttributeError: 'defaultHandler' object has no attribute '_prepared_future'
Called default handler
ERROR:tornado.application:Uncaught exception
Traceback (most recent call last):
  File "C:\Users\******\lib\site-packages\tornado\http1connection.py", line 273, in _read_message
    delegate.finish()
  File "C:\Users\******\lib\site-packages\tornado\routing.py", line 268, in finish
    self.delegate.finish()
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 2297, in finish
    self.execute()
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 2337, in execute
    return self.handler._prepared_future
AttributeError: 'defaultHandler' object has no attribute '_prepared_future'
ERROR:tornado.application:Error in exception logger
Traceback (most recent call last):
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 1665, in _execute
    if self.request.method not in self.SUPPORTED_METHODS:
AttributeError: 'function' object has no attribute 'method'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 1752, in _handle_request_exception
    self.log_exception(*sys.exc_info())
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 1790, in log_exception
    self._request_summary(),
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 1740, in _request_summary
    self.request.method,
AttributeError: 'function' object has no attribute 'method'
ERROR:tornado.application:Exception in exception handler
Traceback (most recent call last):
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 1665, in _execute
    if self.request.method not in self.SUPPORTED_METHODS:
AttributeError: 'function' object has no attribute 'method'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 1708, in _execute
    self._handle_request_exception(e)
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 1757, in _handle_request_exception
    if self._finished:
AttributeError: 'defaultHandler' object has no attribute '_finished'
ERROR:asyncio:Exception in callback _HandlerDelegate.execute.<locals>.<lambda>(<Task finishe...ed_future'",)>) at C:\Users\******\lib\site-packages\tornado\web.py:2333
handle: <Handle _HandlerDelegate.execute.<locals>.<lambda>(<Task finishe...ed_future'",)>) at C:\Users\******\lib\site-packages\tornado\web.py:2333>
Traceback (most recent call last):
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 1665, in _execute
    if self.request.method not in self.SUPPORTED_METHODS:
AttributeError: 'function' object has no attribute 'method'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\******\lib\asyncio\events.py", line 145, in _run
    self._callback(*self._args)
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 2333, in <lambda>
    fut.add_done_callback(lambda f: f.result())
  File "C:\Users\******\lib\site-packages\tornado\web.py", line 1714, in _execute
    if self._prepared_future is not None and not self._prepared_future.done():
AttributeError: 'defaultHandler' object has no attribute '_prepared_future'

What am I doing wrong?我究竟做错了什么? I can't find any example of a working 404 page with python tornado.我找不到任何使用 python 龙卷风的工作 404 页面的示例。 I looked into routers, but that seemed not to fit here.我查看了路由器,但这似乎不适合这里。

You should never override __init__ without calling the superclass's __init__ method.如果不调用超类的__init__方法,则永远不应覆盖__init__ RequestHandler.__init__ takes two arguments which you must pass along. RequestHandler.__init__需要两个 arguments 您必须传递。 For this reason, it is conventional to override the RequestHandler.initialize method instead of __init__ .出于这个原因,通常重写RequestHandler.initialize方法而不是__init__ But it's not clear what arg2 and arg3 are in this example, so I'm not sure why you're overriding this method at all.但是不清楚这个例子中的 arg2 和 arg3 是什么,所以我不确定你为什么要覆盖这个方法。

A conventional 404 handler would look like this:传统的 404 处理程序如下所示:

class DefaultHandler(RequestHandler):
    def prepare(self):
        # Use prepare() to handle all the HTTP methods
        self.set_status(404)
        self.finish("404 - my thing")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM