当我尝试使用Django REST框架POST到一个简单的测试应用程序时,我收到错误"CSRF cookie not set".我已经尝试过Django 1.4和Django 1.6.2.我正在使用Django REST框架v 2.3.13.
我试过使用@csrf_exempt
装饰器,但它没有帮助.
这是一个非常简单的应用程序,没有用户注册/登录等.
有什么想法我收到这个错误?
更新:我已经更新了我的urls.py,如下所示,它现在正在工作!
这是我的代码:
urls.py
from django.conf.urls import patterns, url from quickstart import views urlpatterns = patterns('', url(r'^api_add/$', views.api_add, name='api_add'), )
views.py
from rest_framework import status from rest_framework.decorators import api_view from rest_framework.response import Response @api_view(['POST']) def api_add(request): return Response({"test": 'abc'})
settings.py
INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', )
post.sh
curl -X POST -H "Content-Type: application/json" -d ' { "name": "Manager", "description": "someone who manages" }' http://127.0.0.1:8000/api_add/
tjati.. 9
使用@csrf_exempt
-decorator:
from django.views.decorators.csrf import csrf_exempt @api_view(['POST']) @csrf_exempt def api_add(request): return Response({"test": 'abc'})
更新:
如果您从不需要csrf
-checks,请删除中间件.Seach for MIDDLEWARE_CLASSES
in settings.py
和remove 'django.middleware.csrf.CsrfViewMiddleware',
.
使用@csrf_exempt
-decorator:
from django.views.decorators.csrf import csrf_exempt @api_view(['POST']) @csrf_exempt def api_add(request): return Response({"test": 'abc'})
更新:
如果您从不需要csrf
-checks,请删除中间件.Seach for MIDDLEWARE_CLASSES
in settings.py
和remove 'django.middleware.csrf.CsrfViewMiddleware',
.