summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAntoine Catton <devel@antoine.catton.fr>2014-02-13 20:48:44 -0700
committerTim Graham <timograham@gmail.com>2014-02-14 17:58:03 -0500
commite3d0790bd0b036ed3589659c1196e2c571e3dd8e (patch)
tree444e40bb570bab491384187d3b421c7c7383fde5
parentf3805f5c529d788f72f68d80b492d059cc62e6b2 (diff)
Fixed #21177 -- Made resolve_url support relative URLs.
This fixes redirecting to relative URLs with django.shortcuts.redirect.
-rw-r--r--django/core/urlresolvers.py4
-rw-r--r--docs/releases/1.7.txt3
-rw-r--r--docs/topics/http/shortcuts.txt9
-rw-r--r--tests/resolve_url/tests.py10
4 files changed, 24 insertions, 2 deletions
diff --git a/django/core/urlresolvers.py b/django/core/urlresolvers.py
index 1dca15b4aa..8000300055 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 7359e09ac1..1fe657dfc5 100644
--- a/docs/releases/1.7.txt
+++ b/docs/releases/1.7.txt
@@ -677,6 +677,9 @@ Requests
* The new :attr:`HttpRequest.scheme <django.http.HttpRequest.scheme>` attribute
specifies the scheme of the request (``http`` or ``https`` normally).
+* The shortcut :func:`redirect() <django.shortcuts.redirect>` now supports
+ relative URLs.
+
Tests
^^^^^
diff --git a/docs/topics/http/shortcuts.txt b/docs/topics/http/shortcuts.txt
index c4f413e28c..454aba0ed0 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
<django.core.urlresolvers.reverse>` 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 a29b4dedb0..4fdf2a9fe1 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