Function decorators with parameters on a class based view in Django

蹲街弑〆低调 提交于 2019-12-03 10:42:47

@method_decorator takes a function as parameter. If you want to pass a decorator with parameters, you only need to:

  • Evaluate the parameters in the decorator-creator function.
  • Pass the evaluated value to @method_decorator.

In explicit Python code this would be:

decorator = mydecorator(arg1, arg2, arg...)
method_dec = method_decorator(decorator)

class MyClass(View):
    @method_dec
    def my_view(request):
        ...

So, using the syntactic sugar completely:

class MyClass(View):
    @method_decorator(mydecorator(arg1, arg2, arg...))
    def my_view(request):
        ...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!