Search code examples
pythontornado

Tornado's reverse_url to a fully qualified URL


Tornado's reverse_url is not a fully qualified URL. Is there a mechanism in Tornado to get back a fully qualified URL? For instance:

>>> some_method('foo', 1234)
http://localhost:8080/foo/1234

Solution

  • This is a small helper method which I add to all my handlers:

    from urllib.parse import urljoin  # urlparse in Python 2
    
    class BaseHandler(tornado.web.RequestHandler):
    
        def reverse_full_url(self, name, *args, **kwargs):
            host_url = "{protocol}://{host}".format(**vars(self.request))
            return urljoin(host_url, self.reverse_url(name, *args, **kwargs))