Ticket #5925: 5925.2.diff

File 5925.2.diff, 3.3 KB (added by Preston Timmons, 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
    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/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.
     821.. versionadded:: 1.3
     823A lazily evaluated version of `reverse()`_.
     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:
     828* in your URL configuration (such as the ``url`` argument for the
     829  ``django.views.generic.simple.redirect_to`` generic view).
     831* providing a reversed URL to a decorator (such as the ``login_url`` argument
     832  for the ``django.contrib.auth.decorators.permission_required`` decorator).
  • 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
     5from views import empty_view
     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'))),
  • 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))
     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, "/", status_code=301)
    221228class ReverseShortcutTests(TestCase):
    222229    urls = 'regressiontests.urlpatterns_reverse.urls'
Back to Top