Fixed #24145 -- Added PUT & PATCH to CommonMiddleware APPEND_SLASH redirect error.

This commit is contained in:
Samuel Colvin 2015-01-13 13:42:21 +00:00 committed by Tim Graham
parent eb4cdfbdd6
commit 5b74134f27
2 changed files with 17 additions and 8 deletions

View File

@ -75,14 +75,14 @@ class CommonMiddleware(object):
if (not urlresolvers.is_valid_path(request.path_info, urlconf) and if (not urlresolvers.is_valid_path(request.path_info, urlconf) and
urlresolvers.is_valid_path("%s/" % request.path_info, urlconf)): urlresolvers.is_valid_path("%s/" % request.path_info, urlconf)):
new_url[1] = new_url[1] + '/' 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(("" 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 " "in a slash and you have APPEND_SLASH set. Django can't "
"redirect to the slash URL while maintaining POST data. " "redirect to the slash URL while maintaining %(method)s data. "
"Change your form to point to %s%s (note the trailing " "Change your form to point to %(url)s (note the trailing "
"slash), or set APPEND_SLASH=False in your Django " "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: if new_url == old_url:
# No redirects required. # No redirects required.

View File

@ -68,12 +68,21 @@ class CommonMiddlewareTest(TestCase):
def test_append_slash_no_redirect_on_POST_in_DEBUG(self): def test_append_slash_no_redirect_on_POST_in_DEBUG(self):
""" """
Tests that while in debug mode, an exception is raised with a warning 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 when a failed attempt is made to POST, PUT, or PATCH to an URL which
redirected to a slashed version. 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 = self.rf.get('/slash')
request.method = 'POST' 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) CommonMiddleware().process_request(request)
@override_settings(APPEND_SLASH=False) @override_settings(APPEND_SLASH=False)