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)
>>> get_current_url(env, root_only=True)
>>> get_current_url(env, host_only=True)
>>> get_current_url(env, strip_querystring=True)

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