@csrf_exempt stopped working in Django 1.4

前端 未结 3 1899
臣服心动
臣服心动 2021-02-05 11:14

I have the following code, that was working fine in Django 1.2.5:

from django.views.decorators.csrf import csrf_exempt

class ApiView(object):
    def __call__(s         


        
3条回答
  •  长情又很酷
    2021-02-05 11:58

    According to the django docs:

    To decorate every instance of a class-based view, you need to decorate the class definition itself. To do this you apply the decorator to the dispatch() method of the class.

    So you'd need to do something like:

    class MyView(ApiView):
    
        def POST(self):
           # (...)
           return HttpResponse(json.dumps(True), mimetype="text/javascript")
    
        @csrf_exempt
        def dispatch(self, *args, **kwargs):
            return super(MyView, self).dispatch(*args, **kwargs)
    

提交回复
热议问题