Skip to content

Commit 2ed90ea

Browse files
committed
Fixed django#19779 -- Checked contrib.sites presence in RedirectFallbackMiddleware
Thanks Aymeric Augustin for the report and directions for the patch.
1 parent e486475 commit 2ed90ea

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

django/contrib/redirects/middleware.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
from __future__ import unicode_literals
22

3+
from django.conf import settings
34
from django.contrib.redirects.models import Redirect
45
from django.contrib.sites.models import get_current_site
6+
from django.core.exceptions import ImproperlyConfigured
57
from django import http
6-
from django.conf import settings
8+
79

810
class RedirectFallbackMiddleware(object):
11+
def __init__(self):
12+
if 'django.contrib.sites' not in settings.INSTALLED_APPS:
13+
raise ImproperlyConfigured(
14+
"You cannot use RedirectFallbackMiddleware when "
15+
"django.contrib.sites is not installed."
16+
)
17+
918
def process_response(self, request, response):
1019
if response.status_code != 404:
1120
return response # No need to check for a redirect for non-404 responses.

django/contrib/redirects/tests.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from django.conf import settings
22
from django.contrib.sites.models import Site
3+
from django.core.exceptions import ImproperlyConfigured
34
from django.test import TestCase
45
from django.test.utils import override_settings
56
from django.utils import six
67

8+
from .middleware import RedirectFallbackMiddleware
79
from .models import Redirect
810

911

@@ -52,3 +54,10 @@ def test_response_gone(self):
5254
site=self.site, old_path='/initial', new_path='')
5355
response = self.client.get('/initial')
5456
self.assertEqual(response.status_code, 410)
57+
58+
@override_settings(
59+
INSTALLED_APPS=[app for app in settings.INSTALLED_APPS
60+
if app != 'django.contrib.sites'])
61+
def test_sites_not_installed(self):
62+
with self.assertRaises(ImproperlyConfigured):
63+
RedirectFallbackMiddleware()

docs/ref/contrib/redirects.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ Installation
1313

1414
To install the redirects app, follow these steps:
1515

16-
1. Add ``'django.contrib.redirects'`` to your :setting:`INSTALLED_APPS`
17-
setting.
18-
2. Add ``'django.contrib.redirects.middleware.RedirectFallbackMiddleware'``
16+
1. Ensure that the ``django.contrib.sites`` framework
17+
:ref:`is installed <enabling-the-sites-framework>`.
18+
2. Add ``'django.contrib.redirects'`` to your :setting:`INSTALLED_APPS` setting.
19+
3. Add ``'django.contrib.redirects.middleware.RedirectFallbackMiddleware'``
1920
to your :setting:`MIDDLEWARE_CLASSES` setting.
20-
3. Run the command :djadmin:`manage.py syncdb <syncdb>`.
21+
4. Run the command :djadmin:`manage.py syncdb <syncdb>`.
2122

2223
How it works
2324
============

docs/ref/contrib/sites.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ To do this, you can use the sites framework. A simple example::
246246
>>> 'http://%s%s' % (Site.objects.get_current().domain, obj.get_absolute_url())
247247
'http://example.com/mymodel/objects/3/'
248248

249+
.. _enabling-the-sites-framework:
249250

250251
Enabling the sites framework
251252
============================

docs/releases/1.5.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,12 @@ Miscellaneous
653653
Attempting to load it with ``{% load adminmedia %}`` will fail. If your
654654
templates still contain that line you must remove it.
655655

656+
* Because of an implementation oversight, it was possible to use
657+
:doc:`django.contrib.redirects </ref/contrib/redirects>` without enabling
658+
:doc:`django.contrib.sites </ref/contrib/sites>`. This isn't allowed any
659+
longer. If you're using ``django.contrib.redirects``, make sure
660+
:setting:``INSTALLED_APPS`` contains ``django.contrib.sites``.
661+
656662
Features deprecated in 1.5
657663
==========================
658664

0 commit comments

Comments
 (0)