Ticket #5925: 5925.2.2.diff

File 5925.2.2.diff, 3.3 KB (added by Preston Timmons, 14 years ago)
  • django/core/urlresolvers.py

    diff --git a/django/core/urlresolvers.py b/django/core/urlresolvers.py
    index a35287e..52d9090 100644
    a b from django.conf import settings  
    1515from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist
    1616from django.utils.datastructures import MultiValueDict
    1717from django.utils.encoding import iri_to_uri, force_unicode, smart_str
    18 from django.utils.functional import memoize
     18from django.utils.functional import memoize, lazy
    1919from django.utils.importlib import import_module
    2020from django.utils.regex_helper import normalize
    2121
    def reverse(viewname, urlconf=None, args=None, kwargs=None, prefix=None, current  
    390390    return iri_to_uri(u'%s%s' % (prefix, resolver.reverse(view,
    391391            *args, **kwargs)))
    392392
     393reverse_lazy = lazy(reverse, str)
     394
    393395def clear_url_caches():
    394396    global _resolver_cache
    395397    global _callable_cache
  • docs/topics/http/urls.txt

    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  
    815815    be imported correctly. Do not include lines that reference views you
    816816    haven't written yet, because those views will not be importable.
    817817
     818reverse_lazy()
     819--------------
     820
     821.. versionadded:: 1.3
     822
     823A lazily evaluated version of `reverse()`_.
     824
     825It is useful for when you need to use a URL reversal before Django's URL names
     826map 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
    818834resolve()
    819835---------
    820836
  • new file tests/regressiontests/urlpatterns_reverse/reverse_lazy_urls.py

    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
    - +  
     1from django.conf.urls.defaults import *
     2from django.core.urlresolvers import reverse_lazy
     3from django.views.generic.simple import redirect_to
     4
     5from views import empty_view
     6
     7urlpatterns = 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)
  • tests/regressiontests/urlpatterns_reverse/tests.py

    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):  
    218218                        else:
    219219                            self.assertEqual(t.name, e['name'], 'Wrong URL name.  Expected "%s", got "%s".' % (e['name'], t.name))
    220220
     221class 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
    221228class ReverseShortcutTests(TestCase):
    222229    urls = 'regressiontests.urlpatterns_reverse.urls'
    223230
Back to Top