Skip to content

Commit

Permalink
[1.6.x] Fixed #22220 -- Added more examples to reverse() documention.
Browse files Browse the repository at this point in the history
Thanks EvilDMP for the suggestions.

Backport of 030dd4f from master
  • Loading branch information
bendavis78 authored and timgraham committed Apr 17, 2014
1 parent 8280361 commit e093060
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions docs/ref/urlresolvers.txt
Expand Up @@ -12,17 +12,38 @@ your code, Django provides the following function:


.. function:: reverse(viewname, [urlconf=None, args=None, kwargs=None, current_app=None]) .. function:: reverse(viewname, [urlconf=None, args=None, kwargs=None, current_app=None])


``viewname`` is either the function name (either a function reference, or the ``viewname`` can be a string containing the Python path to the view object, a
string version of the name, if you used that form in ``urlpatterns``) or the :ref:`URL pattern name <naming-url-patterns>`, or the callable view object.
:ref:`URL pattern name <naming-url-patterns>`. Normally, you won't need to For example, given the following ``url``::
worry about the ``urlconf`` parameter and will only pass in the positional and
keyword arguments to use in the URL matching. For example:: url(r'^archive/$', 'news.views.archive', name='news_archive')

you can use any of the following to reverse the URL::

# using the Python path
reverse('news.views.archive')

# using the named URL
reverse('news_archive')

# passing a callable object
from news import views
reverse(views.archive)

If the URL accepts arguments, you may pass them in ``args``. For example::


from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse


def myview(request): def myview(request):
return HttpResponseRedirect(reverse('arch-summary', args=[1945])) return HttpResponseRedirect(reverse('arch-summary', args=[1945]))


You can also pass ``kwargs`` instead of ``args``. For example::

>>> reverse('admin:app_list', kwargs={'app_label': 'auth'})
'/admin/auth/'

``args`` and ``kwargs`` cannot be passed to ``reverse()`` at the same time.

If no match can be made, ``reverse()`` raises a If no match can be made, ``reverse()`` raises a
:class:`~django.core.urlresolvers.NoReverseMatch` exception. :class:`~django.core.urlresolvers.NoReverseMatch` exception.


Expand All @@ -39,12 +60,9 @@ This ``current_app`` argument is used as a hint to resolve application
namespaces into URLs on specific application instances, according to the namespaces into URLs on specific application instances, according to the
:ref:`namespaced URL resolution strategy <topics-http-reversing-url-namespaces>`. :ref:`namespaced URL resolution strategy <topics-http-reversing-url-namespaces>`.


You can use ``kwargs`` instead of ``args``. For example:: The ``urlconf`` argument is the URLconf module containing the url patterns to
use for reversing. By default, the root URLconf for the current thread is used.


>>> reverse('admin:app_list', kwargs={'app_label': 'auth'})
'/admin/auth/'

``args`` and ``kwargs`` cannot be passed to ``reverse()`` at the same time.


.. admonition:: Make sure your views are all correct. .. admonition:: Make sure your views are all correct.


Expand Down

0 comments on commit e093060

Please sign in to comment.