I have a template filter that performs a very simple task and works well, but I would like to use a decorator on it. Unfortunately the decorator causes a nasty django error that doesn't make any sense...
Code that works:
@register.filter(name="has_network")
def has_network(profile, network):
hasnetworkfunc = getattr(profile, "has_%s" % network)
return hasnetworkfunc()
With Decorator (doesn't work):
@register.filter(name="has_network")
@cache_function(30)
def has_network(profile, network):
hasnetworkfunc = getattr(profile, "has_%s" % network)
return hasnetworkfunc()
Here is the error:
TemplateSyntaxError at /
Caught an exception while rendering: pop from empty list
I have tried setting break points inside the decorator and I am reasonably confident that it is not even being called...
But just in case here is the decorator (I know someone will ask for it)
I replaced the decorator (temporarily) with a mock decorator that does nothing, but I still get the same error
def cache_function(cache_timeout):
def wrapper(fn):
def decorator(*args, **kwargs):
return fn(*args, **kwargs)
return decorator
return wrapper
edit CONFIRMED: It is caused because the decorator takes *args
and **kwargs
? I assume pop()
is being called to ensure filters all take at least one arg?
changing the decorator to this fixes the problem:
def cache_function(cache_timeout):
def wrapper(fn):
def decorator(arg1, arg2):
return fn(arg1, arg2)
return decorator
return wrapper
Unfortunately that ruins the generic nature of the decorator :/ what to do now?