- December 7, 2007
- decorator reverse lazy
- 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.
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
- "Magic Link" Management Command by webology 1 month, 3 weeks ago
- Closest ORM models to a latitude/longitude point by simonw 1 month, 3 weeks ago
- Log the time taken to execute each DB query by kennyx46 1 month, 3 weeks ago
- django database snippet by ItsRLuo 1 month, 4 weeks ago
- Serialize a model instance by chriswedgwood 2 months, 3 weeks ago