mirror of
https://github.com/django/django.git
synced 2024-12-25 10:35:48 +00:00
Fixed #19772 -- Handled APPEND_SLASH correctly in the redirects app.
This commit is contained in:
parent
b5391515cd
commit
64623a2e11
@ -1,3 +1,5 @@
|
|||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
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 import http
|
from django import http
|
||||||
@ -7,17 +9,21 @@ class RedirectFallbackMiddleware(object):
|
|||||||
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.
|
||||||
path = request.get_full_path()
|
|
||||||
|
full_path = request.get_full_path()
|
||||||
current_site = get_current_site(request)
|
current_site = get_current_site(request)
|
||||||
try:
|
|
||||||
r = Redirect.objects.get(site__id__exact=current_site.id, old_path=path)
|
|
||||||
except Redirect.DoesNotExist:
|
|
||||||
r = None
|
r = None
|
||||||
if r is None and settings.APPEND_SLASH:
|
|
||||||
# Try removing the trailing slash.
|
|
||||||
try:
|
try:
|
||||||
r = Redirect.objects.get(site__id__exact=current_site.id,
|
r = Redirect.objects.get(site=current_site, old_path=full_path)
|
||||||
old_path=path[:path.rfind('/')]+path[path.rfind('/')+1:])
|
except Redirect.DoesNotExist:
|
||||||
|
pass
|
||||||
|
if settings.APPEND_SLASH and not request.path.endswith('/'):
|
||||||
|
# Try appending a trailing slash.
|
||||||
|
path_len = len(request.path)
|
||||||
|
full_path = full_path[:path_len] + '/' + full_path[path_len:]
|
||||||
|
try:
|
||||||
|
r = Redirect.objects.get(site=current_site, old_path=full_path)
|
||||||
except Redirect.DoesNotExist:
|
except Redirect.DoesNotExist:
|
||||||
pass
|
pass
|
||||||
if r is not None:
|
if r is not None:
|
||||||
|
@ -8,10 +8,10 @@ from .models import Redirect
|
|||||||
|
|
||||||
|
|
||||||
@override_settings(
|
@override_settings(
|
||||||
SITE_ID=1,
|
APPEND_SLASH=False,
|
||||||
APPEND_SLASH=True,
|
|
||||||
MIDDLEWARE_CLASSES=list(settings.MIDDLEWARE_CLASSES) +
|
MIDDLEWARE_CLASSES=list(settings.MIDDLEWARE_CLASSES) +
|
||||||
['django.contrib.redirects.middleware.RedirectFallbackMiddleware'],
|
['django.contrib.redirects.middleware.RedirectFallbackMiddleware'],
|
||||||
|
SITE_ID=1,
|
||||||
)
|
)
|
||||||
class RedirectTests(TestCase):
|
class RedirectTests(TestCase):
|
||||||
|
|
||||||
@ -23,20 +23,32 @@ class RedirectTests(TestCase):
|
|||||||
site=self.site, old_path='/initial', new_path='/new_target')
|
site=self.site, old_path='/initial', new_path='/new_target')
|
||||||
self.assertEqual(six.text_type(r1), "/initial ---> /new_target")
|
self.assertEqual(six.text_type(r1), "/initial ---> /new_target")
|
||||||
|
|
||||||
def test_redirect_middleware(self):
|
def test_redirect(self):
|
||||||
r1 = Redirect.objects.create(
|
Redirect.objects.create(
|
||||||
site=self.site, old_path='/initial', new_path='/new_target')
|
site=self.site, old_path='/initial', new_path='/new_target')
|
||||||
response = self.client.get('/initial')
|
response = self.client.get('/initial')
|
||||||
self.assertRedirects(response,
|
self.assertRedirects(response,
|
||||||
'/new_target', status_code=301, target_status_code=404)
|
'/new_target', status_code=301, target_status_code=404)
|
||||||
# Works also with trailing slash
|
|
||||||
response = self.client.get('/initial/')
|
@override_settings(APPEND_SLASH=True)
|
||||||
|
def test_redirect_with_append_slash(self):
|
||||||
|
Redirect.objects.create(
|
||||||
|
site=self.site, old_path='/initial/', new_path='/new_target/')
|
||||||
|
response = self.client.get('/initial')
|
||||||
self.assertRedirects(response,
|
self.assertRedirects(response,
|
||||||
'/new_target', status_code=301, target_status_code=404)
|
'/new_target/', status_code=301, target_status_code=404)
|
||||||
|
|
||||||
|
@override_settings(APPEND_SLASH=True)
|
||||||
|
def test_redirect_with_append_slash_and_query_string(self):
|
||||||
|
Redirect.objects.create(
|
||||||
|
site=self.site, old_path='/initial/?foo', new_path='/new_target/')
|
||||||
|
response = self.client.get('/initial?foo')
|
||||||
|
self.assertRedirects(response,
|
||||||
|
'/new_target/', status_code=301, target_status_code=404)
|
||||||
|
|
||||||
def test_response_gone(self):
|
def test_response_gone(self):
|
||||||
"""When the redirect target is '', return a 410"""
|
"""When the redirect target is '', return a 410"""
|
||||||
r1 = Redirect.objects.create(
|
Redirect.objects.create(
|
||||||
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)
|
||||||
|
Loading…
Reference in New Issue
Block a user