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
| Attribute | Type | Description |
|---|
| pattern | [URLPattern](urlpattern.md?sid=django_urls_resolvers_urlpattern) | The URL pattern object used to match the incoming request path. |
| urlconf_name | `string | object |
| callback | `callable | null` = null |
| default_kwargs | dict = {} | A dictionary of default keyword arguments to be passed to the retrieved view function. |
| namespace | `string | null` |
| app_name | `string | null` |
| 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_dict | dict | A mapping of namespaces to their corresponding prefixes and sub-resolver objects. |
| app_dict | dict | A mapping of application names to lists of their registered namespaces. |
| urlconf_module | module | The imported Python module containing the URL configuration. |
| url_patterns | list | The 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
| Name | Type | Description |
|---|
| pattern | object | The pattern object used for matching URLs. |
| urlconf_name | `string | object` |
| default_kwargs | dict = None | Additional keyword arguments to be passed to the view function. |
| app_name | string = None | The name of the application for the URL configuration. |
| namespace | string = None | The 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
| Type | Description |
|---|
array | A 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
| Type | Description |
|---|
object | A 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
| Type | Description |
|---|
object | A 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
| Type | Description |
|---|
object | A 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
| Name | Type | Description |
|---|
| path | string | The URL path string to be resolved into a view. |
Returns
| Type | Description |
|---|
object | A 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
| Type | Description |
|---|
module | The 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
| Type | Description |
|---|
array | A 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
| Name | Type | Description |
|---|
| view_type | string | The HTTP error code or type used to look up the handler (e.g., '404'). |
Returns
| Type | Description |
|---|
callable | The 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
| Name | Type | Description |
|---|
| lookup_view | string | The name of the view or the view function to reverse. |
| args | array | Positional arguments to be interpolated into the URL pattern. |
| kwargs | object | Keyword arguments to be interpolated into the URL pattern. |
Returns
| Type | Description |
|---|
string | The absolute URL path corresponding to the provided view and arguments. |