summaryrefslogtreecommitdiff
path: root/docs/ref/urlresolvers.txt
diff options
context:
space:
mode:
Diffstat (limited to 'docs/ref/urlresolvers.txt')
-rw-r--r--docs/ref/urlresolvers.txt23
1 files changed, 13 insertions, 10 deletions
diff --git a/docs/ref/urlresolvers.txt b/docs/ref/urlresolvers.txt
index 264d747f4d..89564279a1 100644
--- a/docs/ref/urlresolvers.txt
+++ b/docs/ref/urlresolvers.txt
@@ -17,30 +17,32 @@ callable view object. For example, given the following ``url``::
from news import views
- path('archive/', views.archive, name='news-archive')
+ path("archive/", views.archive, name="news-archive")
you can use any of the following to reverse the URL::
# using the named URL
- reverse('news-archive')
+ reverse("news-archive")
# passing a callable object
# (This is discouraged because you can't reverse namespaced views this way.)
from news import views
+
reverse(views.archive)
If the URL accepts arguments, you may pass them in ``args``. For example::
from django.urls import reverse
+
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:
.. code-block:: pycon
- >>> reverse('admin:app_list', kwargs={'app_label': 'auth'})
+ >>> reverse("admin:app_list", kwargs={"app_label": "auth"})
'/admin/auth/'
``args`` and ``kwargs`` cannot be passed to ``reverse()`` at the same time.
@@ -71,7 +73,7 @@ use for reversing. By default, the root URLconf for the current thread is used.
.. code-block:: pycon
- >>> reverse('cities', args=['Orléans'])
+ >>> reverse("cities", args=["Orléans"])
'.../Orl%C3%A9ans/'
Applying further encoding (such as :func:`urllib.parse.quote`) to the output
@@ -190,13 +192,13 @@ A :class:`ResolverMatch` object can then be interrogated to provide
information about the URL pattern that matches a URL::
# Resolve a URL
- match = resolve('/some/path/')
+ match = resolve("/some/path/")
# Print the URL pattern that matches the URL
print(match.url_name)
A :class:`ResolverMatch` object can also be assigned to a triple::
- func, args, kwargs = resolve('/some/path/')
+ func, args, kwargs = resolve("/some/path/")
One possible use of :func:`~django.urls.resolve` would be to test whether a
view would raise a ``Http404`` error before redirecting to it::
@@ -205,19 +207,20 @@ view would raise a ``Http404`` error before redirecting to it::
from django.urls import resolve
from django.http import Http404, HttpResponseRedirect
+
def myview(request):
- next = request.META.get('HTTP_REFERER', None) or '/'
+ next = request.META.get("HTTP_REFERER", None) or "/"
response = HttpResponseRedirect(next)
# modify the request and response as required, e.g. change locale
# and set corresponding locale cookie
view, args, kwargs = resolve(urlparse(next)[2])
- kwargs['request'] = request
+ kwargs["request"] = request
try:
view(*args, **kwargs)
except Http404:
- return HttpResponseRedirect('/')
+ return HttpResponseRedirect("/")
return response
``get_script_prefix()``