MCPcopy
hub / github.com/django/django / URLPattern

Class URLPattern

django/urls/resolvers.py:422–500  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

420
421
422class URLPattern:
423 def __init__(self, pattern, callback, default_args=None, name=None):
424 self.pattern = pattern
425 self.callback = callback # the view
426 self.default_args = default_args or {}
427 self.name = name
428
429 def __repr__(self):
430 return "<%s %s>" % (self.__class__.__name__, self.pattern.describe())
431
432 def check(self):
433 warnings = self._check_pattern_name()
434 warnings.extend(self.pattern.check())
435 warnings.extend(self._check_callback())
436 return warnings
437
438 def _check_pattern_name(self):
439 """
440 Check that the pattern name does not contain a colon.
441 """
442 if self.pattern.name is not None and ":" in self.pattern.name:
443 warning = Warning(
444 "Your URL pattern {} has a name including a ':'. Remove the colon, to "
445 "avoid ambiguous namespace references.".format(self.pattern.describe()),
446 id="urls.W003",
447 )
448 return [warning]
449 else:
450 return []
451
452 def _check_callback(self):
453 from django.views import View
454
455 view = self.callback
456 if inspect.isclass(view) and issubclass(view, View):
457 return [
458 Error(
459 "Your URL pattern %s has an invalid view, pass %s.as_view() "
460 "instead of %s."
461 % (
462 self.pattern.describe(),
463 view.__name__,
464 view.__name__,
465 ),
466 id="urls.E009",
467 )
468 ]
469 return []
470
471 def resolve(self, path):
472 match = self.pattern.match(path)
473 if match:
474 new_path, args, captured_kwargs = match
475 # Pass any default args as **kwargs.
476 kwargs = {**captured_kwargs, **self.default_args}
477 return ResolverMatch(
478 self.callback,
479 args,

Callers 1

_pathFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…