daniel daniel - 11 months ago 52
Python Question

Tornado's reverse_url to a fully qualified 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)

Answer Source

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