How to disable Django's CSRF validation?

前端 未结 9 1338
梦毁少年i
梦毁少年i 2020-11-30 21:23

I have commented out csrf processor and middleware lines in settings.py:

122 
123 TEMPLATE_CONTEXT_PROCESSORS = (
124     \'django.contrib.auth.         


        
9条回答
  •  借酒劲吻你
    2020-11-30 22:16

    If you want disable it in Global, you can write a custom middleware, like this

    from django.utils.deprecation import MiddlewareMixin
    
    class DisableCsrfCheck(MiddlewareMixin):
    
        def process_request(self, req):
            attr = '_dont_enforce_csrf_checks'
            if not getattr(req, attr, False):
                setattr(req, attr, True)
    

    then add this class youappname.middlewarefilename.DisableCsrfCheck to MIDDLEWARE_CLASSES lists, before django.middleware.csrf.CsrfViewMiddleware

提交回复
热议问题