Loading django/middleware/common.py +6 −1 Original line number Diff line number Diff line Loading @@ -30,11 +30,16 @@ class CommonMiddleware(object): urlpatterns, then an HTTP-redirect is returned to this new URL; otherwise the initial URL is processed as usual. This behavior can be customized by subclassing CommonMiddleware and overriding the response_redirect_class attribute. - ETags: If the USE_ETAGS setting is set, ETags will be calculated from the entire page content and Not Modified responses will be returned appropriately. """ response_redirect_class = http.HttpResponsePermanentRedirect def process_request(self, request): """ Check for denied User-Agents and rewrite the URL based on Loading Loading @@ -100,7 +105,7 @@ class CommonMiddleware(object): newurl += '?' + request.META['QUERY_STRING'].decode() except UnicodeDecodeError: pass return http.HttpResponsePermanentRedirect(newurl) return self.response_redirect_class(newurl) def process_response(self, request, response): """ Loading docs/ref/middleware.txt +8 −0 Original line number Diff line number Diff line Loading @@ -66,6 +66,14 @@ Adds a few conveniences for perfectionists: for each request by MD5-hashing the page content, and it'll take care of sending ``Not Modified`` responses, if appropriate. .. attribute:: CommonMiddleware.response_redirect_class .. versionadded:: 1.8 Defaults to :class:`~django.http.HttpResponsePermanentRedirect`. Subclass ``CommonMiddleware`` and override the attribute to customize the redirects issued by the middleware. .. class:: BrokenLinkEmailsMiddleware * Sends broken link notification emails to :setting:`MANAGERS` (see Loading docs/releases/1.8.txt +7 −0 Original line number Diff line number Diff line Loading @@ -304,6 +304,13 @@ Management Commands :setting:`FIXTURE_DIRS` contains duplicates or a default fixture directory path (``app_name/fixtures``), an exception is raised. Middleware ^^^^^^^^^^ * The :attr:`CommonMiddleware.response_redirect_class <django.middleware.common.CommonMiddleware.response_redirect_class>` attribute allows you to customize the redirects issued by the middleware. Migrations ^^^^^^^^^^ Loading tests/middleware/tests.py +21 −1 Original line number Diff line number Diff line Loading @@ -9,7 +9,10 @@ from unittest import skipIf from django.conf import settings from django.core import mail from django.http import HttpRequest, HttpResponse, StreamingHttpResponse from django.http import ( HttpRequest, HttpResponse, StreamingHttpResponse, HttpResponsePermanentRedirect, HttpResponseRedirect, ) from django.middleware.clickjacking import XFrameOptionsMiddleware from django.middleware.common import CommonMiddleware, BrokenLinkEmailsMiddleware from django.middleware.http import ConditionalGetMiddleware Loading Loading @@ -242,6 +245,23 @@ class CommonMiddlewareTest(TestCase): response = CommonMiddleware().process_request(request) self.assertEqual(response.status_code, 301) def test_response_redirect_class(self): request = self._get_request('slash') r = CommonMiddleware().process_request(request) self.assertEqual(r.status_code, 301) self.assertEqual(r.url, 'http://testserver/slash/') self.assertIsInstance(r, HttpResponsePermanentRedirect) def test_response_redirect_class_subclass(self): class MyCommonMiddleware(CommonMiddleware): response_redirect_class = HttpResponseRedirect request = self._get_request('slash') r = MyCommonMiddleware().process_request(request) self.assertEqual(r.status_code, 302) self.assertEqual(r.url, 'http://testserver/slash/') self.assertIsInstance(r, HttpResponseRedirect) @override_settings( IGNORABLE_404_URLS=(re.compile(r'foo'),), Loading Loading
django/middleware/common.py +6 −1 Original line number Diff line number Diff line Loading @@ -30,11 +30,16 @@ class CommonMiddleware(object): urlpatterns, then an HTTP-redirect is returned to this new URL; otherwise the initial URL is processed as usual. This behavior can be customized by subclassing CommonMiddleware and overriding the response_redirect_class attribute. - ETags: If the USE_ETAGS setting is set, ETags will be calculated from the entire page content and Not Modified responses will be returned appropriately. """ response_redirect_class = http.HttpResponsePermanentRedirect def process_request(self, request): """ Check for denied User-Agents and rewrite the URL based on Loading Loading @@ -100,7 +105,7 @@ class CommonMiddleware(object): newurl += '?' + request.META['QUERY_STRING'].decode() except UnicodeDecodeError: pass return http.HttpResponsePermanentRedirect(newurl) return self.response_redirect_class(newurl) def process_response(self, request, response): """ Loading
docs/ref/middleware.txt +8 −0 Original line number Diff line number Diff line Loading @@ -66,6 +66,14 @@ Adds a few conveniences for perfectionists: for each request by MD5-hashing the page content, and it'll take care of sending ``Not Modified`` responses, if appropriate. .. attribute:: CommonMiddleware.response_redirect_class .. versionadded:: 1.8 Defaults to :class:`~django.http.HttpResponsePermanentRedirect`. Subclass ``CommonMiddleware`` and override the attribute to customize the redirects issued by the middleware. .. class:: BrokenLinkEmailsMiddleware * Sends broken link notification emails to :setting:`MANAGERS` (see Loading
docs/releases/1.8.txt +7 −0 Original line number Diff line number Diff line Loading @@ -304,6 +304,13 @@ Management Commands :setting:`FIXTURE_DIRS` contains duplicates or a default fixture directory path (``app_name/fixtures``), an exception is raised. Middleware ^^^^^^^^^^ * The :attr:`CommonMiddleware.response_redirect_class <django.middleware.common.CommonMiddleware.response_redirect_class>` attribute allows you to customize the redirects issued by the middleware. Migrations ^^^^^^^^^^ Loading
tests/middleware/tests.py +21 −1 Original line number Diff line number Diff line Loading @@ -9,7 +9,10 @@ from unittest import skipIf from django.conf import settings from django.core import mail from django.http import HttpRequest, HttpResponse, StreamingHttpResponse from django.http import ( HttpRequest, HttpResponse, StreamingHttpResponse, HttpResponsePermanentRedirect, HttpResponseRedirect, ) from django.middleware.clickjacking import XFrameOptionsMiddleware from django.middleware.common import CommonMiddleware, BrokenLinkEmailsMiddleware from django.middleware.http import ConditionalGetMiddleware Loading Loading @@ -242,6 +245,23 @@ class CommonMiddlewareTest(TestCase): response = CommonMiddleware().process_request(request) self.assertEqual(response.status_code, 301) def test_response_redirect_class(self): request = self._get_request('slash') r = CommonMiddleware().process_request(request) self.assertEqual(r.status_code, 301) self.assertEqual(r.url, 'http://testserver/slash/') self.assertIsInstance(r, HttpResponsePermanentRedirect) def test_response_redirect_class_subclass(self): class MyCommonMiddleware(CommonMiddleware): response_redirect_class = HttpResponseRedirect request = self._get_request('slash') r = MyCommonMiddleware().process_request(request) self.assertEqual(r.status_code, 302) self.assertEqual(r.url, 'http://testserver/slash/') self.assertIsInstance(r, HttpResponseRedirect) @override_settings( IGNORABLE_404_URLS=(re.compile(r'foo'),), Loading