def middleware_on_class_with_args(middleware): """ Same as ``middleware_on_class`` but returns a function that accepts additional arguments to be passed to middleware Use as: @middleware_on_class_with_args(MiddlewareClass)(arg0, kwarg=x) class View: ... The decorated class is modified in place. """ def wrapper_builder(*args, **kwargs): def decorator(cls): dispatch = cls.dispatch @wraps(dispatch) @method_decorator(decorator_from_middleware_with_args(middleware)(*args, **kwargs)) def wrapper(self, *args, **kwargs): return dispatch(self, *args, **kwargs) cls.dispatch = wrapper return cls return decorator return wrapper_builder def middleware_on_class(middleware): """ A class decorator to attach the designated middleware to a class-based view. Use as @middleware_on_class(MiddlewareClass) class View: ... Takes a single parameter, ``MiddlewareClass'', which must be a django middleware class (not an instance). The decorated class is modified in place. """ def decorator(cls): dispatch = cls.dispatch @wraps(dispatch) @method_decorator(decorator_from_middleware(middleware)) def wrapper(self, *args, **kwargs): return dispatch(self, *args, **kwargs) cls.dispatch = wrapper return cls