Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unset csrf in modelviewset of django-rest-framework?

How to unset csrf in modelviewset of django-rest-framework?

I'll use viewsets.ModelViewSet(http://django-rest-framework.org/api-guide/viewsets.html#modelviewset) of django-rest-framework.

And my app is api server. So I don't need to use csrf.

But I don't know how to unset csrf.

Please give me a example!

like image 475
chobo Avatar asked Nov 11 '13 02:11

chobo


People also ask

How do I exempt CSRF token in Django?

By setting the cookie and using a corresponding token, subdomains will be able to circumvent the CSRF protection. The only way to avoid this is to ensure that subdomains are controlled by trusted users (or, are at least unable to set cookies).

Does REST framework need CSRF token?

If you're using SessionAuthentication you'll need to include valid CSRF tokens for any POST , PUT , PATCH or DELETE operations. In order to make AJAX requests, you need to include CSRF token in the HTTP header, as described in the Django documentation.

What is ModelViewSet in Django REST framework?

ModelViewSet. The ModelViewSet class inherits from GenericAPIView and includes implementations for various actions, by mixing in the behavior of the various mixin classes. The actions provided by the ModelViewSet class are .list() , .retrieve() , .create() , .update() , .partial_update() , and .destroy() .

What is difference between APIView and Viewset?

APIView allow us to define functions that match standard HTTP methods like GET, POST, PUT, PATCH, etc. Viewsets allow us to define functions that match to common API object actions like : LIST, CREATE, RETRIEVE, UPDATE, etc.


1 Answers

CSRF is only enforced if you're using SessionAuthentication. If you're using one of the other form of authentication (eg TokenAuthentication) then it won't be required.

like image 66
Tom Christie Avatar answered Oct 21 '22 11:10

Tom Christie