diff --git a/django/core/urlresolvers.py b/django/core/urlresolvers.py
index a35287e..52d9090 100644
a
|
b
|
from django.conf import settings
|
15 | 15 | from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist |
16 | 16 | from django.utils.datastructures import MultiValueDict |
17 | 17 | from django.utils.encoding import iri_to_uri, force_unicode, smart_str |
18 | | from django.utils.functional import memoize |
| 18 | from django.utils.functional import memoize, lazy |
19 | 19 | from django.utils.importlib import import_module |
20 | 20 | from django.utils.regex_helper import normalize |
21 | 21 | |
… |
… |
def reverse(viewname, urlconf=None, args=None, kwargs=None, prefix=None, current
|
390 | 390 | return iri_to_uri(u'%s%s' % (prefix, resolver.reverse(view, |
391 | 391 | *args, **kwargs))) |
392 | 392 | |
| 393 | reverse_lazy = lazy(reverse, str) |
| 394 | |
393 | 395 | def clear_url_caches(): |
394 | 396 | global _resolver_cache |
395 | 397 | global _callable_cache |
diff --git a/docs/topics/http/urls.txt b/docs/topics/http/urls.txt
index 091dcb0..c82226a 100644
a
|
b
|
namespaces into URLs on specific application instances, according to the
|
815 | 815 | be imported correctly. Do not include lines that reference views you |
816 | 816 | haven't written yet, because those views will not be importable. |
817 | 817 | |
| 818 | reverse_lazy() |
| 819 | -------------- |
| 820 | |
| 821 | .. versionadded:: 1.3 |
| 822 | |
| 823 | A lazily evaluated version of `reverse()`_. |
| 824 | |
| 825 | It is useful for when you need to use a URL reversal before Django's URL names |
| 826 | map is loaded. Some common cases where this method is necessary are: |
| 827 | |
| 828 | * in your URL configuration (such as the ``url`` argument for the |
| 829 | ``django.views.generic.simple.redirect_to`` generic view). |
| 830 | |
| 831 | * providing a reversed URL to a decorator (such as the ``login_url`` argument |
| 832 | for the ``django.contrib.auth.decorators.permission_required`` decorator). |
| 833 | |
818 | 834 | resolve() |
819 | 835 | --------- |
820 | 836 | |
diff --git a/tests/regressiontests/urlpatterns_reverse/reverse_lazy_urls.py b/tests/regressiontests/urlpatterns_reverse/reverse_lazy_urls.py
new file mode 100644
index 0000000..aa9d0f2
-
|
+
|
|
| 1 | from django.conf.urls.defaults import * |
| 2 | from django.core.urlresolvers import reverse_lazy |
| 3 | from django.views.generic.simple import redirect_to |
| 4 | |
| 5 | from views import empty_view |
| 6 | |
| 7 | urlpatterns = patterns('', |
| 8 | url(r'^$', empty_view, name='named-lazy-url-1'), |
| 9 | url(r'^redirect/$', redirect_to, dict(url=reverse_lazy('named-lazy-url-1'))), |
| 10 | ) |
diff --git a/tests/regressiontests/urlpatterns_reverse/tests.py b/tests/regressiontests/urlpatterns_reverse/tests.py
index 198d556..48b03d9 100644
a
|
b
|
class ResolverTests(unittest.TestCase):
|
218 | 218 | else: |
219 | 219 | self.assertEqual(t.name, e['name'], 'Wrong URL name. Expected "%s", got "%s".' % (e['name'], t.name)) |
220 | 220 | |
| 221 | class ReverseLazyTest(TestCase): |
| 222 | urls = 'regressiontests.urlpatterns_reverse.reverse_lazy_urls' |
| 223 | |
| 224 | def test_redirect_with_lazy_reverse(self): |
| 225 | response = self.client.get('/redirect/') |
| 226 | self.assertRedirects(response, "/", status_code=301) |
| 227 | |
221 | 228 | class ReverseShortcutTests(TestCase): |
222 | 229 | urls = 'regressiontests.urlpatterns_reverse.urls' |