diff options
| author | Sjoerd Job Postmus <sjoerdjob@sjec.nl> | 2016-10-20 19:29:04 +0200 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2017-09-20 18:04:42 -0400 |
| commit | df41b5a05d4e00e80e73afe629072e37873e767a (patch) | |
| tree | baaf71ae695e2d3af604ea0d663284cb406c71e4 /django/urls/conf.py | |
| parent | c4c128d67c7dc2830631c6859a204c9d259f1fb1 (diff) | |
Fixed #28593 -- Added a simplified URL routing syntax per DEP 0201.
Thanks Aymeric Augustin for shepherding the DEP and patch review.
Thanks Marten Kenbeek and Tim Graham for contributing to the code.
Thanks Tom Christie, Shai Berger, and Tim Graham for the docs.
Diffstat (limited to 'django/urls/conf.py')
| -rw-r--r-- | django/urls/conf.py | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/django/urls/conf.py b/django/urls/conf.py index 99bff55819..119e95df41 100644 --- a/django/urls/conf.py +++ b/django/urls/conf.py @@ -1,9 +1,12 @@ """Functions for use in URLsconfs.""" +from functools import partial from importlib import import_module from django.core.exceptions import ImproperlyConfigured -from .resolvers import LocaleRegexURLResolver +from .resolvers import ( + LocalePrefixPattern, RegexPattern, RoutePattern, URLPattern, URLResolver, +) def include(arg, namespace=None): @@ -43,8 +46,32 @@ def include(arg, namespace=None): # testcases will break). if isinstance(patterns, (list, tuple)): for url_pattern in patterns: - if isinstance(url_pattern, LocaleRegexURLResolver): + pattern = getattr(url_pattern, 'pattern', None) + if isinstance(pattern, LocalePrefixPattern): raise ImproperlyConfigured( 'Using i18n_patterns in an included URLconf is not allowed.' ) return (urlconf_module, app_name, namespace) + + +def _path(route, view, kwargs=None, name=None, Pattern=None): + if isinstance(view, (list, tuple)): + # For include(...) processing. + pattern = Pattern(route, is_endpoint=False) + urlconf_module, app_name, namespace = view + return URLResolver( + pattern, + urlconf_module, + kwargs, + app_name=app_name, + namespace=namespace, + ) + elif callable(view): + pattern = Pattern(route, name=name, is_endpoint=True) + return URLPattern(pattern, view, kwargs, name) + else: + raise TypeError('view must be a callable or a list/tuple in the case of include().') + + +path = partial(_path, Pattern=RoutePattern) +re_path = partial(_path, Pattern=RegexPattern) |
