I developed a simple web service but couldn't use the post with the Django Rest Framework as it complains about CSRF:
"detail": "CSRF Failed: CSRF cookie not set."
Removing the api_view decorator stops the message from appearing, but then I will not be able to access request.data. I think api_view checks for CSRF, although I added the csrf_exempt decorator.
It's my opinion:
@permission_classes((IsAuthenticated, )) @csrf_exempt @api_view(['POST']) def get_stats(request): """ Returns the stats available. """ user = request.user if request.method == 'POST': serializer = StatsRequestSerializer(data=request.data) stats_request = serializer.data return JSONResponse(stats_request)
This is my model:
class StatsRequest(models.Model): """ A model which describes a request for some stats for specific users. """ start_date = models.DateField() end_date = models.DateField()
and this is my POST request:
{"start_date" : "1992-01-15", "end_date" : "1992-01-15" }
Any ideas?
Additional Information:
AUTHENTICATION_BACKENDS = ( 'social.backends.facebook.FacebookOAuth2', 'social.backends.google.GoogleOAuth2', 'django.contrib.auth.backends.ModelBackend' )
python django csrf
Shynet 04 Sep '15 at 13:03 2015-09-04 13:03
source share