diff options
| author | Carlton Gibson <carlton.gibson@noumenal.es> | 2020-10-20 09:14:48 +0200 |
|---|---|---|
| committer | Carlton Gibson <carlton@noumenal.es> | 2020-10-22 14:15:19 +0200 |
| commit | ad11f5b8c9cbfdb763e08c83170694abc0c5fc1e (patch) | |
| tree | 28647127cf0bc05120bee7417609ec997a5b1f96 /django | |
| parent | 34180922380cf41cd684f846ecf00f92eb289bcf (diff) | |
Fixed #32124 -- Added per-view opt-out for APPEND_SLASH behavior.
Diffstat (limited to 'django')
| -rw-r--r-- | django/middleware/common.py | 9 | ||||
| -rw-r--r-- | django/urls/base.py | 9 | ||||
| -rw-r--r-- | django/views/decorators/common.py | 14 |
3 files changed, 23 insertions, 9 deletions
diff --git a/django/middleware/common.py b/django/middleware/common.py index e6f30f44ad..3af4759109 100644 --- a/django/middleware/common.py +++ b/django/middleware/common.py @@ -67,10 +67,11 @@ class CommonMiddleware(MiddlewareMixin): """ if settings.APPEND_SLASH and not request.path_info.endswith('/'): urlconf = getattr(request, 'urlconf', None) - return ( - not is_valid_path(request.path_info, urlconf) and - is_valid_path('%s/' % request.path_info, urlconf) - ) + if not is_valid_path(request.path_info, urlconf): + match = is_valid_path('%s/' % request.path_info, urlconf) + if match: + view = match.func + return getattr(view, 'should_append_slash', True) return False def get_full_path_with_slash(self, request): diff --git a/django/urls/base.py b/django/urls/base.py index 3899feeefb..6cf75d3a3f 100644 --- a/django/urls/base.py +++ b/django/urls/base.py @@ -145,13 +145,12 @@ def get_urlconf(default=None): def is_valid_path(path, urlconf=None): """ - Return True if the given path resolves against the default URL resolver, - False otherwise. This is a convenience method to make working with "is - this a match?" cases easier, avoiding try...except blocks. + Return the ResolverMatch if the given path resolves against the default URL + resolver, False otherwise. This is a convenience method to make working + with "is this a match?" cases easier, avoiding try...except blocks. """ try: - resolve(path, urlconf) - return True + return resolve(path, urlconf) except Resolver404: return False diff --git a/django/views/decorators/common.py b/django/views/decorators/common.py new file mode 100644 index 0000000000..34b0e5a50e --- /dev/null +++ b/django/views/decorators/common.py @@ -0,0 +1,14 @@ +from functools import wraps + + +def no_append_slash(view_func): + """ + Mark a view function as excluded from CommonMiddleware's APPEND_SLASH + redirection. + """ + # view_func.should_append_slash = False would also work, but decorators are + # nicer if they don't have side effects, so return a new function. + def wrapped_view(*args, **kwargs): + return view_func(*args, **kwargs) + wrapped_view.should_append_slash = False + return wraps(view_func)(wrapped_view) |
