[Fixed]-Django Custom View Decorators


You’re missing a step in the decorator, or rather you have a step confused. It’s the outer function that must return the inner function (_wrapped_view_func), and it must always do so: that’s what takes the place of the original function when it is called.

I’m not sure what the except clause is there for. Apart from it always being a bad idea to use a blank except – that catches everything, including things like ctrl-c – exceptions in Django functions are usually handled by the middleware, rather than the decorator. I would just remove it.

So the code should be:

def no_m(view_func):
    def _wrapped_view_func(request, *args, **kwargs): 
        if request.user.is_m():     
            # quick test
            return HttpResponseRedirect('http://google.com')            
             return view_func(request, *args, **kwargs)     
    return _wrapped_view_func

Leave a comment