TypeError: initialize() missing 1 required positional argument: 'url'

TypeError: initialize() missing 1 required positional argument: 'url'

我不知道这个错误是什么:在__init__ self.initialize(**kwargs) # 输入:忽略 类型错误:初始化()缺少 1 个必需的位置参数:'url'

我正在使用 python 作为后端。我是新来的。在此代码中,我使用的是 tornado web。是的,这段代码正在调试,但是当我在浏览器上打开 localhost:8882/ & localhost:8882/animals 时,它显示了这个错误。请帮助我

我的index.py页面代码:-

import tornado.web
import tornado.ioloop


class basicRequestHandler(tornado.web.RedirectHandler):
    def get(self):
        self.write('Hello, World this is python Command from backend')


class listRequestHandler(tornado.web.RedirectHandler):
    def get(self):
        self.request.render('index.html')


if __name__ == "__main__":
    app = tornado.web.Application([
        (r"/",basicRequestHandler),
        (r"/animals", listRequestHandler),
    ])
        
    port = 8882
    app.listen(port)
    print(f"Application is ready and listening on port {port}")
    tornado.ioloop.IOLoop.current().start()

我的 index.html 页面是:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>List of Animals</title>
</head>
<body>
    <h1>This is the list of Animals</h1>
    <select>
        <option>Cat</option>
        <option>Horse</option>
        <option>Rat</option>
        <option>Cow</option>
    </select>
</body>
</html>

我认为你应该使用 tornado.web.RequestHandler 而不是 tornado.web.RedirectHandler
basicRequestHandlerlistRequestHandler 中编辑它,使其匹配以下内容:

class basicRequestHandler(tornado.web.RequestHandler):
    def get(self):
        self.write('Hello, World this is python Command from backend')


class listRequestHandler(tornado.web.RequestHandler):
    def get(self):
        self.request.render('index.html')


然后你会得到以下错误:

AttributeError: 'HTTPServerRequest' object has no attribute 'render'

这是因为你做的是self.request.render('index.html'),需要是self.render('index.html'),所以你的最终代码是这样的:

import tornado.web
import tornado.ioloop


class basicRequestHandler(tornado.web.RequestHandler):
    def get(self):
        self.write('Hello, World this is python Command from backend')


class listRequestHandler(tornado.web.RequestHandler):
    def get(self):
        self.render('index.html')


if __name__ == "__main__":
    app = tornado.web.Application([
        (r"/",basicRequestHandler),
        (r"/animals", listRequestHandler),
    ])

    port = 8882
    app.listen(port)
    print(f"Application is ready and listening on port {port}")
    tornado.ioloop.IOLoop.current().start()