mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	[1.5.x] Fixed #19779 -- Checked contrib.sites presence in RedirectFallbackMiddleware
Thanks Aymeric Augustin for the report and directions for the patch.
Backport of 2ed90eac from master.
			
			
This commit is contained in:
		| @@ -1,11 +1,20 @@ | |||||||
| from __future__ import unicode_literals | from __future__ import unicode_literals | ||||||
|  |  | ||||||
|  | from django.conf import settings | ||||||
| from django.contrib.redirects.models import Redirect | from django.contrib.redirects.models import Redirect | ||||||
| from django.contrib.sites.models import get_current_site | from django.contrib.sites.models import get_current_site | ||||||
|  | from django.core.exceptions import ImproperlyConfigured | ||||||
| from django import http | from django import http | ||||||
| from django.conf import settings |  | ||||||
|  |  | ||||||
| class RedirectFallbackMiddleware(object): | class RedirectFallbackMiddleware(object): | ||||||
|  |     def __init__(self): | ||||||
|  |         if 'django.contrib.sites' not in settings.INSTALLED_APPS: | ||||||
|  |             raise ImproperlyConfigured( | ||||||
|  |                 "You cannot use RedirectFallbackMiddleware when " | ||||||
|  |                 "django.contrib.sites is not installed." | ||||||
|  |             ) | ||||||
|  |  | ||||||
|     def process_response(self, request, response): |     def process_response(self, request, response): | ||||||
|         if response.status_code != 404: |         if response.status_code != 404: | ||||||
|             return response # No need to check for a redirect for non-404 responses. |             return response # No need to check for a redirect for non-404 responses. | ||||||
|   | |||||||
| @@ -1,9 +1,11 @@ | |||||||
| from django.conf import settings | from django.conf import settings | ||||||
| from django.contrib.sites.models import Site | from django.contrib.sites.models import Site | ||||||
|  | from django.core.exceptions import ImproperlyConfigured | ||||||
| from django.test import TestCase | from django.test import TestCase | ||||||
| from django.test.utils import override_settings | from django.test.utils import override_settings | ||||||
| from django.utils import six | from django.utils import six | ||||||
|  |  | ||||||
|  | from .middleware import RedirectFallbackMiddleware | ||||||
| from .models import Redirect | from .models import Redirect | ||||||
|  |  | ||||||
|  |  | ||||||
| @@ -52,3 +54,10 @@ class RedirectTests(TestCase): | |||||||
|             site=self.site, old_path='/initial', new_path='') |             site=self.site, old_path='/initial', new_path='') | ||||||
|         response = self.client.get('/initial') |         response = self.client.get('/initial') | ||||||
|         self.assertEqual(response.status_code, 410) |         self.assertEqual(response.status_code, 410) | ||||||
|  |  | ||||||
|  |     @override_settings( | ||||||
|  |         INSTALLED_APPS=[app for app in settings.INSTALLED_APPS | ||||||
|  |                         if app != 'django.contrib.sites']) | ||||||
|  |     def test_sites_not_installed(self): | ||||||
|  |         with self.assertRaises(ImproperlyConfigured): | ||||||
|  |             RedirectFallbackMiddleware() | ||||||
|   | |||||||
| @@ -13,11 +13,12 @@ Installation | |||||||
|  |  | ||||||
| To install the redirects app, follow these steps: | To install the redirects app, follow these steps: | ||||||
|  |  | ||||||
| 1. Add ``'django.contrib.redirects'`` to your :setting:`INSTALLED_APPS` | 1. Ensure that the ``django.contrib.sites`` framework | ||||||
|    setting. |    :ref:`is installed <enabling-the-sites-framework>`. | ||||||
| 2. Add ``'django.contrib.redirects.middleware.RedirectFallbackMiddleware'`` | 2. Add ``'django.contrib.redirects'`` to your :setting:`INSTALLED_APPS` setting. | ||||||
|  | 3. Add ``'django.contrib.redirects.middleware.RedirectFallbackMiddleware'`` | ||||||
|    to your :setting:`MIDDLEWARE_CLASSES` setting. |    to your :setting:`MIDDLEWARE_CLASSES` setting. | ||||||
| 3. Run the command :djadmin:`manage.py syncdb <syncdb>`. | 4. Run the command :djadmin:`manage.py syncdb <syncdb>`. | ||||||
|  |  | ||||||
| How it works | How it works | ||||||
| ============ | ============ | ||||||
|   | |||||||
| @@ -246,6 +246,7 @@ To do this, you can use the sites framework. A simple example:: | |||||||
|     >>> 'http://%s%s' % (Site.objects.get_current().domain, obj.get_absolute_url()) |     >>> 'http://%s%s' % (Site.objects.get_current().domain, obj.get_absolute_url()) | ||||||
|     'http://example.com/mymodel/objects/3/' |     'http://example.com/mymodel/objects/3/' | ||||||
|  |  | ||||||
|  | .. _enabling-the-sites-framework: | ||||||
|  |  | ||||||
| Default site and ``syncdb`` | Default site and ``syncdb`` | ||||||
| =========================== | =========================== | ||||||
|   | |||||||
| @@ -653,6 +653,12 @@ Miscellaneous | |||||||
|   Attempting to load it with ``{% load adminmedia %}`` will fail. If your |   Attempting to load it with ``{% load adminmedia %}`` will fail. If your | ||||||
|   templates still contain that line you must remove it. |   templates still contain that line you must remove it. | ||||||
|  |  | ||||||
|  | * Because of an implementation oversight, it was possible to use | ||||||
|  |   :doc:`django.contrib.redirects </ref/contrib/redirects>` without enabling | ||||||
|  |   :doc:`django.contrib.sites </ref/contrib/sites>`. This isn't allowed any | ||||||
|  |   longer. If you're using ``django.contrib.redirects``, make sure | ||||||
|  |   :setting:``INSTALLED_APPS`` contains ``django.contrib.sites``. | ||||||
|  |  | ||||||
| Features deprecated in 1.5 | Features deprecated in 1.5 | ||||||
| ========================== | ========================== | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user