Professional Documents
Culture Documents
.. note::
The string returned by ``reverse()`` is already
:ref:`urlquoted <uri-and-iri-handling>`. For example::
>>> reverse('cities', args=[u'Orlans'])
'.../Orl%C3%A9ans/'
Applying further encoding (such as :meth:`~django.utils.http.urlquote` or
``urllib.quote``) to the output of ``reverse()`` may produce undesirable
results.
reverse_lazy()
-------------.. versionadded:: 1.4
A lazily evaluated version of `reverse()`_.
.. function:: reverse_lazy(viewname, [urlconf=None, args=None, kwargs=None, curr
ent_app=None])
It is useful for when you need to use a URL reversal before your project's
URLConf is loaded. Some common cases where this function is necessary are:
* providing a reversed URL as the ``url`` attribute of a generic class-based
view.
* providing a reversed URL to a decorator (such as the ``login_url`` argument
for the :func:`django.contrib.auth.decorators.permission_required`
decorator).
* providing a reversed URL as a default value for a parameter in a function's
signature.
resolve()
--------The ``resolve()`` function can be used for resolving URL paths to the
corresponding view functions. It has the following signature:
.. function:: resolve(path, urlconf=None)
``path`` is the URL path you want to resolve. As with
:func:`~django.core.urlresolvers.reverse`, you don't need to
worry about the ``urlconf`` parameter. The function returns a
:class:`ResolverMatch` object that allows you
to access various meta-data about the resolved URL.
If the URL does not resolve, the function raises an
:class:`~django.http.Http404` exception.
.. class:: ResolverMatch
.. attribute:: ResolverMatch.func
The view function that would be used to serve the URL
.. attribute:: ResolverMatch.args
return HttpResponseRedirect('/')
return response
get_script_prefix()
------------------.. function:: get_script_prefix()
Normally, you should always use :func:`~django.core.urlresolvers.reverse` to
define URLs within your application. However, if your application constructs
part of the URL hierarchy itself, you may occasionally need to generate URLs.
In that case, you need to be able to find the base URL of the Django project
within its Web server (normally, :func:`~django.core.urlresolvers.reverse`
takes care of this for you). In that case, you can call
``get_script_prefix()``, which will return the script prefix portion of the URL
for your Django project. If your Django project is at the root of its web
server, this is always ``"/"``.