From 5b74134f27eabf92870e1c5e81f9e4999f113eab Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Tue, 13 Jan 2015 13:42:21 +0000 Subject: [PATCH] Fixed #24145 -- Added PUT & PATCH to CommonMiddleware APPEND_SLASH redirect error. --- django/middleware/common.py | 10 +++++----- tests/middleware/tests.py | 15 ++++++++++++--- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/django/middleware/common.py b/django/middleware/common.py index 0bf0481df61..0531d31908c 100644 --- a/django/middleware/common.py +++ b/django/middleware/common.py @@ -75,14 +75,14 @@ class CommonMiddleware(object): if (not urlresolvers.is_valid_path(request.path_info, urlconf) and urlresolvers.is_valid_path("%s/" % request.path_info, urlconf)): new_url[1] = new_url[1] + '/' - if settings.DEBUG and request.method == 'POST': + if settings.DEBUG and request.method in ('POST', 'PUT', 'PATCH'): raise RuntimeError(("" - "You called this URL via POST, but the URL doesn't end " + "You called this URL via %(method)s, but the URL doesn't end " "in a slash and you have APPEND_SLASH set. Django can't " - "redirect to the slash URL while maintaining POST data. " - "Change your form to point to %s%s (note the trailing " + "redirect to the slash URL while maintaining %(method)s data. " + "Change your form to point to %(url)s (note the trailing " "slash), or set APPEND_SLASH=False in your Django " - "settings.") % (new_url[0], new_url[1])) + "settings.") % {'method': request.method, 'url': ''.join(new_url)}) if new_url == old_url: # No redirects required. diff --git a/tests/middleware/tests.py b/tests/middleware/tests.py index 2b5b50f9e5b..30ed394503e 100644 --- a/tests/middleware/tests.py +++ b/tests/middleware/tests.py @@ -68,12 +68,21 @@ class CommonMiddlewareTest(TestCase): def test_append_slash_no_redirect_on_POST_in_DEBUG(self): """ Tests that while in debug mode, an exception is raised with a warning - when a failed attempt is made to POST to an URL which would normally be - redirected to a slashed version. + when a failed attempt is made to POST, PUT, or PATCH to an URL which + would normally be redirected to a slashed version. """ + msg = "maintaining %s data. Change your form to point to testserver/slash/" request = self.rf.get('/slash') request.method = 'POST' - with six.assertRaisesRegex(self, RuntimeError, 'end in a slash'): + with six.assertRaisesRegex(self, RuntimeError, msg % request.method): + CommonMiddleware().process_request(request) + request = self.rf.get('/slash') + request.method = 'PUT' + with six.assertRaisesRegex(self, RuntimeError, msg % request.method): + CommonMiddleware().process_request(request) + request = self.rf.get('/slash') + request.method = 'PATCH' + with six.assertRaisesRegex(self, RuntimeError, msg % request.method): CommonMiddleware().process_request(request) @override_settings(APPEND_SLASH=False)