2

I have such api method:

@api_view(['POST'])
@login_required
def get_posts(request):
    # ...

How can I disable CSRF only on this method?

1 Answer 1

6

For function based views you can usually use the decorator csrf_exempt:

from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse

@csrf_exempt
def my_view(request):
    return HttpResponse('Hello world')

Update: There may be an exception for the DRF. Take a look here.

Sign up to request clarification or add additional context in comments.

2 Comments

Why did you remove @api_view() from your example?
Because api_view is only for DRF. I wanted to explain that the decorator csrf_exempt is not DRF specific. So I updated the answer and linked it to another SO question, which explains another related caveat (authentication) in more detail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.