@csrf_exempt stopped working in Django 1.4

前端 未结 3 1896
臣服心动
臣服心动 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)
    
    0 讨论(0)
  • 2021-02-05 12:00

    Just use csrf_exempt in the urls.py. ie::

    urls.py

    ..other imports...
    from django.views.decorators.csrf import csrf_exempt   
    from myapp.views import MyView
    
    urlpatterns = patterns('',
       url(r'^myview/(?P<parameter_name>[A-Za-z0-9-_]+)/$',
           csrf_exempt(MyView.as_view()), # use csrf_exempt here
           name="myproject-myapp-myview",
           ),
    )
    
    0 讨论(0)
  • 2021-02-05 12:02

    csrf_exempt has to decorate a function. In your urls you can decorate a that function, docs can be found here.

    (r'^vote/', permission_required('polls.can_vote')(VoteView.as_view())),
    
    0 讨论(0)
提交回复
热议问题