Skip to main content

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

AttributeTypeDescription
patternobjectThe compiled URL pattern object used to match incoming request paths against defined routes.
callbackcallableThe view function or class-based view used to handle requests when the URL pattern is successfully matched.
default_argsdict = {}A dictionary of additional keyword arguments passed to the view function during request resolution.
namestringThe unique identifier for the URL pattern used for reversing URLs and performing lookups.
lookup_strstringA 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

NameTypeDescription
patternobjectThe pattern object used for matching URLs.
callbackcallableThe view function or class-based view to be executed when the pattern matches.
default_argsdict = NoneA dictionary of additional arguments to pass to the callback function.
namestr = NoneThe 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

TypeDescription
listA 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

NameTypeDescription
pathstringThe URL path string to be matched against the pattern.

Returns

TypeDescription
[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

TypeDescription
stringThe full Python import path to the view function or class associated with this pattern.