Skip to main content

URLResolver

This class maps URL paths to view functions and handles the reverse process of generating URLs from view identifiers. It supports nested URL configurations, namespaces, and application-specific routing by recursively resolving patterns against a provided URL configuration module. Additionally, it manages thread-safe population of lookup dictionaries to facilitate efficient URL reversing and pattern matching.

Attributes

AttributeTypeDescription
pattern[URLPattern](urlpattern.md?sid=django_urls_resolvers_urlpattern)The URL pattern object used to match the incoming request path.
urlconf_name`stringobject
callback`callablenull` = null
default_kwargsdict = {}A dictionary of default keyword arguments to be passed to the retrieved view function.
namespace`stringnull`
app_name`stringnull`
reverse_dict[MultiValueDict](../../utils/datastructures/multivaluedict.md?sid=django_utils_datastructures_multivaluedict)A mapping of URL names and callables to their respective path patterns and defaults, used for reversing URLs.
namespace_dictdictA mapping of namespaces to their corresponding prefixes and sub-resolver objects.
app_dictdictA mapping of application names to lists of their registered namespaces.
urlconf_modulemoduleThe imported Python module containing the URL configuration.
url_patternslistThe list of URL patterns defined in the associated URLconf module.

Constructor

Signature

def URLResolver(
pattern: object,
urlconf_name: string | object,
default_kwargs: dict = None,
app_name: string = None,
namespace: string = None
) - > null

Parameters

NameTypeDescription
patternobjectThe pattern object used for matching URLs.
urlconf_name`stringobject`
default_kwargsdict = NoneAdditional keyword arguments to be passed to the view function.
app_namestring = NoneThe name of the application for the URL configuration.
namespacestring = NoneThe instance namespace for the URL configuration.

Methods


check()

@classmethod
def check() - > array

Validates the URL patterns within the resolver and its associated pattern object to identify configuration errors.

Returns

TypeDescription
arrayA list of system check messages or errors found in the URL configuration.

reverse_dict()

@classmethod
def reverse_dict() - > object

Retrieves the mapping of view names to URL patterns for the current language, triggering population if necessary.

Returns

TypeDescription
objectA MultiValueDict containing URL patterns indexed by view name or callback.

namespace_dict()

@classmethod
def namespace_dict() - > object

Retrieves the mapping of namespaces to their corresponding URL prefixes and resolvers for the current language.

Returns

TypeDescription
objectA dictionary where keys are namespace strings and values are tuples of (prefix, resolver).

app_dict()

@classmethod
def app_dict() - > object

Retrieves the mapping of application names to their registered namespaces for the current language.

Returns

TypeDescription
objectA dictionary where keys are application names and values are lists of associated namespaces.

resolve()

@classmethod
def resolve(
path: string
) - > object

Attempts to match a URL path against the registered patterns and returns the matching view and arguments.

Parameters

NameTypeDescription
pathstringThe URL path string to be resolved into a view.

Returns

TypeDescription
objectA ResolverMatch object containing the view function, arguments, and URL metadata.

urlconf_module()

@classmethod
def urlconf_module() - > module

Imports and returns the Python module containing the URL patterns defined for this resolver.

Returns

TypeDescription
moduleThe Python module object containing the urlpatterns.

url_patterns()

@classmethod
def url_patterns() - > array

Retrieves the list of URL patterns from the configured module, raising an error if the module is misconfigured.

Returns

TypeDescription
arrayA list of URLPattern or URLResolver instances.

resolve_error_handler()

@classmethod
def resolve_error_handler(
view_type: string
) - > callable

Locates the appropriate error handler view for a specific HTTP error type (e.g., 404, 500).

Parameters

NameTypeDescription
view_typestringThe HTTP error code or type used to look up the handler (e.g., '404').

Returns

TypeDescription
callableThe view function responsible for handling the specified error type.

reverse()

@classmethod
def reverse(
lookup_view: string,
args: array,
kwargs: object
) - > string

Generates a URL path from a view name or callback by performing a reverse lookup with an empty prefix.

Parameters

NameTypeDescription
lookup_viewstringThe name of the view or the view function to reverse.
argsarrayPositional arguments to be interpolated into the URL pattern.
kwargsobjectKeyword arguments to be interpolated into the URL pattern.

Returns

TypeDescription
stringThe absolute URL path corresponding to the provided view and arguments.