首页 > 解决方案 > 无法理解 django csrf 豁免 cbv 的代码

问题描述

我无法理解调度方法的含义以及method_decorator是什么

class CSRFExemptMixin(object):
   @method_decorator(csrf_exempt)
   def dispatch(self, *args, **kwargs):
       return super(CSRFExemptMixin, self).dispatch(*args, **kwargs)

你能回答这个问题吗?

标签: djangopython-3.xdjango-rest-framework

解决方案


Dispatch 只是函数的名称。当某些东西将使用这个类时,这个函数将被触发。而method_decorator只是装饰器。装饰器是一个函数,它接收一个函数,在装饰器内部制作其他东西,然后最终执行接收到的函数。


推荐阅读