Question

I have a custom decorator on a view and i have to cache that view after processing some request variables. My decorator code is like this

def custom_dec(view_func):
    @wraps(view_func, assigned=available_attrs(view_func))
    def wrapper(request,filters,*args,**kwargs):
        # do some processing on request and filters
        return csrf_exempt(cache_page(900, view_func))
return wrapper

and i have applied the decorator as :

@custom_dec
def myview(request,filters,*args,**kwargs):
    # view code here

The problem is running this code gives me an error when going through the middleware :

Exception Type: AttributeError
Exception Value: 'function' object has no attribute 'status_code'

When i look the at respone it's the function myview instead of the response of the view.
response <function myview at 0xb549e534>

What is wrong in my code?

UPDATE: The code runs fine if i change the return in warpper function to return view_func which means i must be doing something wrong in applying cache page decorator.

Was it helpful?

Solution

Turns out i had to return an HttpResponse object. It worked when i changed the code to :

def custom_dec(view_func):
    @wraps(view_func, assigned=available_attrs(view_func))
    def wrapper(request,filters,*args,**kwargs):
        # do some processing on request and filters
        cached_func = cache_page(900, view_func)
        return cached_func(request,filters,*args,**kwargs) #this returns an HttpResponse object
        # the above two line could also be written as cache_page(900, view_func)(request,filters,*args,**kwargs)
return wrapper 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top