From e3d0790bd0b036ed3589659c1196e2c571e3dd8e Mon Sep 17 00:00:00 2001 From: Antoine Catton Date: Thu, 13 Feb 2014 20:48:44 -0700 Subject: [PATCH] Fixed #21177 -- Made resolve_url support relative URLs. This fixes redirecting to relative URLs with django.shortcuts.redirect. --- django/core/urlresolvers.py | 4 ++++ docs/releases/1.7.txt | 3 +++ docs/topics/http/shortcuts.txt | 9 +++++++-- tests/resolve_url/tests.py | 10 ++++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/django/core/urlresolvers.py b/django/core/urlresolvers.py index 1dca15b4aad..8000300055b 100644 --- a/django/core/urlresolvers.py +++ b/django/core/urlresolvers.py @@ -383,6 +383,10 @@ class RegexURLResolver(LocaleRegexProvider): text_args = [force_text(v) for v in args] text_kwargs = dict((k, force_text(v)) for (k, v) in kwargs.items()) + if isinstance(lookup_view, six.string_types): + # Handle relative URLs + if any(lookup_view.startswith(path) for path in ('./', '../')): + return lookup_view try: lookup_view = get_callable(lookup_view, True) except (ImportError, AttributeError) as e: diff --git a/docs/releases/1.7.txt b/docs/releases/1.7.txt index 7359e09ac16..1fe657dfc53 100644 --- a/docs/releases/1.7.txt +++ b/docs/releases/1.7.txt @@ -677,6 +677,9 @@ Requests * The new :attr:`HttpRequest.scheme ` attribute specifies the scheme of the request (``http`` or ``https`` normally). +* The shortcut :func:`redirect() ` now supports + relative URLs. + Tests ^^^^^ diff --git a/docs/topics/http/shortcuts.txt b/docs/topics/http/shortcuts.txt index c4f413e28c9..454aba0ed06 100644 --- a/docs/topics/http/shortcuts.txt +++ b/docs/topics/http/shortcuts.txt @@ -203,10 +203,15 @@ If you want to override the :setting:`TEMPLATE_DIRS` setting, use the ` will be used to reverse-resolve the name. - * A URL, which will be used as-is for the redirect location. + * An absolute or relative URL, which will be used as-is for the redirect + location. By default issues a temporary redirect; pass ``permanent=True`` to issue a - permanent redirect + permanent redirect. + + .. versionchanged:: 1.7 + + The ability to use relative URLs was added. Examples -------- diff --git a/tests/resolve_url/tests.py b/tests/resolve_url/tests.py index a29b4dedb0c..4fdf2a9fe12 100644 --- a/tests/resolve_url/tests.py +++ b/tests/resolve_url/tests.py @@ -21,6 +21,16 @@ class ResolveUrlTests(TestCase): """ self.assertEqual('/something/', resolve_url('/something/')) + def test_relative_path(self): + """ + Tests that passing a relative URL path to ``resolve_url`` will result + in the same url. + """ + self.assertEqual('../', resolve_url('../')) + self.assertEqual('../relative/', resolve_url('../relative/')) + self.assertEqual('./', resolve_url('./')) + self.assertEqual('./relative/', resolve_url('./relative/')) + def test_full_url(self): """ Tests that passing a full URL to ``resolve_url`` will result in the