How can I disable Django's csrf protection only in certain cases?

后端 未结 3 1367
情话喂你
情话喂你 2020-12-29 19:41

I\'m trying to write a site in Django where the API URLs are the same as user-facing URLs. But I\'m having trouble with pages which use POST requests and CSRF protection.

相关标签:
3条回答
  • 2020-12-29 19:59

    There is a section of Django's CSRF Protection documentation titled View needs protection for one path which describes a solution. The idea is to use @csrf_exempt on the whole view, but when the API client header is not present or invalid, then call a function annotated with @csrf_protect.

    0 讨论(0)
  • 2020-12-29 20:02

    If you are you using class base view (CBV) and want to use the csrf_exempt decorator you will need to use the method decorator.

    from django.utils.decorators import method_decorator
    from django.views import View
    from django.views.decorators.csrf import csrf_exempt
    
    @method_decorator(csrf_exempt, name='dispatch')
    class MyView(View):
        def post(self, request):
            pass  # my view code here
    
    0 讨论(0)
  • 2020-12-29 20:16

    Modify urls.py

    If you manage your routes in urls.py, you can wrap your desired routes with csrf_exempt() to exclude them from the CSRF verification middleware.

    for instance,

    from django.views.decorators.csrf import csrf_exempt
    urlpatterns = patterns(
        # ...
        # Will exclude `/api/v1/test` from CSRF 
        url(r'^api/v1/test', csrf_exempt(TestApiHandler.as_view()))
        # ...
    )
    

    Alternatively, as a Decorator

    Some may find the use of the @csrf_exempt decorator more suitable for their needs

    for instance,

    from django.views.decorators.csrf import csrf_exempt
    from django.http import HttpResponse
    
    @csrf_exempt
    def my_view(request):
        return HttpResponse('Hello world')
    
    0 讨论(0)
提交回复
热议问题