URLPattern
This class represents a mapping between a URL pattern and a specific callback view. It provides functionality to resolve incoming URL paths into executable matches and includes validation logic to ensure pattern names and view callbacks are correctly configured. The class also supports optional default arguments and named URL patterns for reverse lookup.
Attributes
| Attribute | Type | Description |
|---|---|---|
| pattern | object | The compiled URL pattern object used to match incoming request paths against defined routes. |
| callback | callable | The view function or class-based view used to handle requests when the URL pattern is successfully matched. |
| default_args | dict = {} | A dictionary of additional keyword arguments passed to the view function during request resolution. |
| name | string | The unique identifier for the URL pattern used for reversing URLs and performing lookups. |
| lookup_str | string | A string that identifies the view (e.g. 'path.to.view_function' or 'path.to.ClassBasedView'). |
Constructor
Signature
def URLPattern(
pattern: object,
callback: callable,
default_args: dict = None,
name: str = None
)
Parameters
| Name | Type | Description |
|---|---|---|
| pattern | object | The pattern object used for matching URLs. |
| callback | callable | The view function or class-based view to be executed when the pattern matches. |
| default_args | dict = None | A dictionary of additional arguments to pass to the callback function. |
| name | str = None | The name of the URL pattern for reverse lookups. |
Methods
check()
@classmethod
def check() - > list
Runs validation checks on the URL pattern name, the pattern itself, and the associated callback view to identify configuration issues.
Returns
| Type | Description |
|---|---|
list | A list of warning and error objects encountered during the validation process. |
resolve()
@classmethod
def resolve(
path: string
) - > [ResolverMatch](resolvermatch.md?sid=django_urls_resolvers_resolvermatch)
Attempts to match the provided URL path against the pattern and returns resolution metadata if successful.
Parameters
| Name | Type | Description |
|---|---|---|
| path | string | The URL path string to be matched against the pattern. |
Returns
| Type | Description |
|---|---|
[ResolverMatch](resolvermatch.md?sid=django_urls_resolvers_resolvermatch) | A ResolverMatch object containing the view, arguments, and pattern metadata if a match is found; otherwise None. |
lookup_str()
@classmethod
def lookup_str() - > string
A string that identifies the view (e.g. 'path.to.view_function' or 'path.to.ClassBasedView').
Returns
| Type | Description |
|---|---|
string | The full Python import path to the view function or class associated with this pattern. |