Login

lazy url reverse()

Author:
guettli
Posted:
December 7, 2007
Language:
Python
Version:
1.3
Score:
2 (after 2 ratings)

Since the decorators of your views are evaluated during parsing urls.py you have an 'chicken - egg' problem. The method reverse() can't be used since urls.py is not read.

This snippets evaluates reverse() lazy.

Related ticket: 5925

Django 1.4 (current trunk) has a lazy reverse.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# file lazy.py
from django.core import urlresolvers

class lazy_string(object):
    def __init__(self, function, *args, **kwargs):
        self.function=function
        self.args=args
        self.kwargs=kwargs
        
    def __str__(self):
        if not hasattr(self, 'str'):
            self.str=self.function(*self.args, **self.kwargs)
        return self.str

def reverse(*args, **kwargs):
    return lazy_string(urlresolvers.reverse, *args, **kwargs)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 5 months ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 5 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 1 year ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year ago
  5. Help text hyperlinks by sa2812 1 year, 1 month ago

Comments

Please login first before commenting.