Ticket #5925: 5925.reverse_lazy.diff

File 5925.reverse_lazy.diff, 4.1 KB (added by Julien Phalip, 12 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
    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)))
     393reverse_lazy = lazy(reverse, str)
    393395def clear_url_caches():
    394396    global _resolver_cache
    395397    global _callable_cache
  • docs/releases/1.4.txt

    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 
    3737features you need in admin pages without having to lose HTML validity or
    3838override the provided templates to change the doctype.
     43A lazily evaluated version of ``django.core.urlresolvers.reverse()`` was added
     44to allow using URL reversals before the project's URLConf gets loaded.
    4046.. _backwards-incompatible-changes-1.4:
    4248Backwards incompatible changes in 1.4
  • docs/topics/http/urls.txt

    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 
    827827    ``urllib.quote``) to the ouput of :meth:`~django.core.urlresolvers.reverse`
    828828    may produce undesirable results.
     833.. versionadded:: 1.4
     835A lazily evaluated version of `reverse()`_.
     837It is useful for when you need to use a URL reversal before your project's
     838URLConf is loaded. Some common cases where this method is necessary are::
     840* providing a reversed URL as the ``url`` attribute of a generic class-based
     841  view.
     843* providing a reversed URL to a decorator (such as the ``login_url`` argument
     844  for the ``django.contrib.auth.decorators.permission_required`` decorator).
     846* providing a reversed URL as a default value for a parameter in a function's
     847  signature.
  • 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..a6a5a5f
    - +  
     1from django.conf.urls.defaults import *
     2from django.core.urlresolvers import reverse_lazy
     3from django.views.generic import RedirectView
     5from views import empty_view
     8class LazyRedictView(RedirectView):
     9    url = reverse_lazy('named-lazy-url-redirected-to')
     11urlpatterns = patterns('',
     12    url(r'^redirected_to/$', empty_view, name='named-lazy-url-redirected-to'),
     13    url(r'^redirect/$', LazyRedictView.as_view()),
  • tests/regressiontests/urlpatterns_reverse/tests.py

    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): 
    218218                        else:
    219219                            self.assertEqual(t.name, e['name'], 'Wrong URL name.  Expected "%s", got "%s".' % (e['name'], t.name))
     221class ReverseLazyTest(TestCase):
     222    urls = 'regressiontests.urlpatterns_reverse.reverse_lazy_urls'
     224    def test_redirect_with_lazy_reverse(self):
     225        response = self.client.get('/redirect/')
     226        self.assertRedirects(response, "/redirected_to/", status_code=301)
    221228class ReverseShortcutTests(TestCase):
    222229    urls = 'regressiontests.urlpatterns_reverse.urls'
Back to Top