Ticket #5925: 5925.reverse_lazy.diff

File 5925.reverse_lazy.diff, 4.1 KB (added by Julien Phalip, 13 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/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.
    3939
     40``reverse_lazy``
     41~~~~~~~~~~~~~~~~
     42
     43A lazily evaluated version of ``django.core.urlresolvers.reverse()`` was added
     44to allow using URL reversals before the project's URLConf gets loaded.
     45
    4046.. _backwards-incompatible-changes-1.4:
    4147
    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.
    829829
     830reverse_lazy()
     831--------------
     832
     833.. versionadded:: 1.4
     834
     835A lazily evaluated version of `reverse()`_.
     836
     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::
     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
    830849resolve()
    831850---------
    832851
  • 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
     4
     5from views import empty_view
     6
     7
     8class LazyRedictView(RedirectView):
     9    url = reverse_lazy('named-lazy-url-redirected-to')
     10
     11urlpatterns = patterns('',
     12    url(r'^redirected_to/$', empty_view, name='named-lazy-url-redirected-to'),
     13    url(r'^redirect/$', LazyRedictView.as_view()),
     14)
  • 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))
    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, "/redirected_to/", status_code=301)
     227
    221228class ReverseShortcutTests(TestCase):
    222229    urls = 'regressiontests.urlpatterns_reverse.urls'
    223230
Back to Top