Commit 5b74134f authored by Samuel Colvin's avatar Samuel Colvin Committed by Tim Graham
Browse files

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

parent eb4cdfbd
Loading
Loading
Loading
Loading
+5 −5
Original line number Diff line number Diff line
@@ -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.
+12 −3
Original line number Diff line number Diff line
@@ -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)