werkzeug.get_current_url

werkzeug.get_current_url(environ, root_only=False, strip_querystring=False, host_only=False, trusted_hosts=None)[source]

A handy helper function that recreates the full URL as IRI for the current request or parts of it. Here an example:

>>> from werkzeug.test import create_environ
>>> env = create_environ("/?param=foo", "http://localhost/script")
>>> get_current_url(env)
'http://localhost/script/?param=foo'
>>> get_current_url(env, root_only=True)
'http://localhost/script/'
>>> get_current_url(env, host_only=True)
'http://localhost/'
>>> get_current_url(env, strip_querystring=True)
'http://localhost/script/'

This optionally it verifies that the host is in a list of trusted hosts. If the host is not in there it will raise a SecurityError.

Note that the string returned might contain unicode characters as the representation is an IRI not an URI. If you need an ASCII only representation you can use the iri_to_uri() function:

>>> from werkzeug.urls import iri_to_uri
>>> iri_to_uri(get_current_url(env))
'http://localhost/script/?param=foo'
Parameters:
  • environ – the WSGI environment to get the current URL from.
  • root_only – set True if you only want the root URL.
  • strip_querystring – set to True if you don’t want the querystring.
  • host_only – set to True if the host URL should be returned.
  • trusted_hosts – a list of trusted hosts, see host_is_trusted() for more information.