class ResolverMatch [source]
-
func -
The view function that would be used to serve the URL
-
args -
The arguments that would be passed to the view function, as parsed from the URL.
-
kwargs -
The keyword arguments that would be passed to the view function, as parsed from the URL.
-
url_name -
The name of the URL pattern that matches the URL.
-
app_name -
The application namespace for the URL pattern that matches the URL.
-
app_names -
New in Django 1.9.
The list of individual namespace components in the full application namespace for the URL pattern that matches the URL. For example, if the
app_nameis'foo:bar', thenapp_nameswill be['foo', 'bar'].
-
namespace -
The instance namespace for the URL pattern that matches the URL.
-
namespaces -
The list of individual namespace components in the full instance namespace for the URL pattern that matches the URL. i.e., if the namespace is
foo:bar, then namespaces will be['foo', 'bar'].
-
view_name -
The name of the view that matches the URL, including the namespace if there is one.
A ResolverMatch object can then be interrogated to provide information about the URL pattern that matches a URL:
# Resolve a URL
match = resolve('/some/path/')
# Print the URL pattern that matches the URL
print(match.url_name)
A ResolverMatch object can also be assigned to a triple:
func, args, kwargs = resolve('/some/path/')
One possible use of resolve() would be to test whether a view would raise a Http404 error before redirecting to it:
from django.urls import resolve
from django.http import HttpResponseRedirect, Http404
from django.utils.six.moves.urllib.parse import urlparse
def myview(request):
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
try:
view(*args, **kwargs)
except Http404:
return HttpResponseRedirect('/')
return response
Please login to continue.