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/releases/1.4.txt b/docs/releases/1.4.txt
index a63fff7..8ceed20 100644
a
|
b
|
compatibility with old browsers, this change means that you can use any HTML5
|
37 | 37 | features you need in admin pages without having to lose HTML validity or |
38 | 38 | override the provided templates to change the doctype. |
39 | 39 | |
| 40 | ``reverse_lazy`` |
| 41 | ~~~~~~~~~~~~~~~~ |
| 42 | |
| 43 | A lazily evaluated version of ``django.core.urlresolvers.reverse()`` was added |
| 44 | to allow using URL reversals before the project's URLConf gets loaded. |
| 45 | |
40 | 46 | .. _backwards-incompatible-changes-1.4: |
41 | 47 | |
42 | 48 | Backwards incompatible changes in 1.4 |
diff --git a/docs/topics/http/urls.txt b/docs/topics/http/urls.txt
index d721012..af66fa8 100644
a
|
b
|
namespaces into URLs on specific application instances, according to the
|
827 | 827 | ``urllib.quote``) to the ouput of :meth:`~django.core.urlresolvers.reverse` |
828 | 828 | may produce undesirable results. |
829 | 829 | |
| 830 | reverse_lazy() |
| 831 | -------------- |
| 832 | |
| 833 | .. versionadded:: 1.4 |
| 834 | |
| 835 | A lazily evaluated version of `reverse()`_. |
| 836 | |
| 837 | It is useful for when you need to use a URL reversal before your project's |
| 838 | URLConf is loaded. Some common cases where this method is necessary are:: |
| 839 | |
| 840 | * providing a reversed URL as the ``url`` attribute of a generic class-based |
| 841 | view. |
| 842 | |
| 843 | * providing a reversed URL to a decorator (such as the ``login_url`` argument |
| 844 | for the ``django.contrib.auth.decorators.permission_required`` decorator). |
| 845 | |
| 846 | * providing a reversed URL as a default value for a parameter in a function's |
| 847 | signature. |
| 848 | |
830 | 849 | resolve() |
831 | 850 | --------- |
832 | 851 | |
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..a6a5a5f
-
|
+
|
|
| 1 | from django.conf.urls.defaults import * |
| 2 | from django.core.urlresolvers import reverse_lazy |
| 3 | from django.views.generic import RedirectView |
| 4 | |
| 5 | from views import empty_view |
| 6 | |
| 7 | |
| 8 | class LazyRedictView(RedirectView): |
| 9 | url = reverse_lazy('named-lazy-url-redirected-to') |
| 10 | |
| 11 | urlpatterns = patterns('', |
| 12 | url(r'^redirected_to/$', empty_view, name='named-lazy-url-redirected-to'), |
| 13 | url(r'^redirect/$', LazyRedictView.as_view()), |
| 14 | ) |
diff --git a/tests/regressiontests/urlpatterns_reverse/tests.py b/tests/regressiontests/urlpatterns_reverse/tests.py
index 198d556..655e36b 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, "/redirected_to/", status_code=301) |
| 227 | |
221 | 228 | class ReverseShortcutTests(TestCase): |
222 | 229 | urls = 'regressiontests.urlpatterns_reverse.urls' |
223 | 230 | |