【问题标题】:TypeError: initialize() missing 1 required positional argument: 'url'类型错误:initialize() 缺少 1 个必需的位置参数:'url'
【发布时间】:2020-04-18 15:01:09
【问题描述】:

我不知道这个错误是什么:in __init__ self.initialize(**kwargs) # 类型:忽略 类型错误:initialize() 缺少 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>

【问题讨论】:

  • 跟踪的完整错误是什么?

标签: python tags tornado


【解决方案1】:

我认为您应该使用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()

【讨论】:

    猜你喜欢
    • 2018-10-29
    • 2017-11-30
    • 2022-01-11
    • 1970-01-01
    • 2018-09-12
    • 2021-08-05
    • 2021-07-06
    • 2021-08-05
    相关资源
    最近更新 更多