Search code examples
pythontagstornado

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


I don't know what this error is: in __init__ self.initialize(**kwargs) # type: ignore TypeError: initialize() missing 1 required positional argument: 'url'

I am using python as a backend. I a new here. In this code, I am using tornado web. Yes this code is debugging but as I open localhost:8882/ & localhost:8882/animals on my browser it shows this error. Please help me

my index.py page code:-

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()

and my index.html page is:

<!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>

Solution

  • I think you should be using tornado.web.RequestHandler instead of tornado.web.RedirectHandler.
    Edit this in basicRequestHandler and listRequestHandler, so it matches the following:

    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')
    
    
    

    Then you'll get the following error:

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

    This is because you do self.request.render('index.html'), which needs to be self.render('index.html'), so your final code looks like this:

    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()