Code

Ticket #9658: resolve_docs.diff

File resolve_docs.diff, 1.6 KB (added by mrts, 5 years ago)
Line 
1Index: docs/topics/http/urls.txt
2===================================================================
3--- docs/topics/http/urls.txt   (revision 9513)
4+++ docs/topics/http/urls.txt   (working copy)
5@@ -633,7 +633,40 @@
6     be imported correctly. Do not include lines that reference views you
7     haven't written yet, because those views will not be importable.
8 
9+resolve()
10+---------
11 
12+The :func:`django.core.urlresolvers.resolve` function can be used for resolving
13+URL paths to the corresponding view functions. It has the following signature:
14+
15+.. currentmodule:: django.core.urlresolvers
16+.. function:: resolve(path, urlconf=None)
17+
18+``path`` is the URL path you want to resolve. As with ``reverse()`` above, you
19+don't need to worry about the ``urlconf`` parameter. The function returns the
20+triple (view function, arguments, keyword arguments).
21+
22+For example, it can be used for testing if a view would raise a ``Http404``
23+error before redirecting to it::
24+
25+    from urlparse import urlparse
26+    from django.core.urlresolvers import resolve
27+    from django.http import HttpResponseRedirect, Http404
28+
29+    def myview(request):
30+        next = request.META.get('HTTP_REFERER', None) or '/'
31+        response = HttpResponseRedirect(next)
32+
33+        # modify the request and response as required, e.g. change locale
34+        # and set corresponding locale cookie
35+
36+        view, args, kwargs = resolve(urlparse(next)[2])
37+        try:
38+            view(*args, **kwargs)
39+        except Http404:
40+            return HttpResponseRedirect('/')
41+        return response
42+
43 permalink()
44 -----------
45