我正在使用chrome postman扩展来测试Django的请求和响应功能,因为我需要将数据发布到django应用程序.我的应用视图是:
def index(request): # loop through keys for key in request.POST: value = request.POST[key] # loop through keys and values output ="" for key, value in request.POST.iteritems(): output= output + str(key) + " " + str(value) + "
" return HttpResponse(output)
当我发送请求时,我得到:
Forbidden (403) CSRF verification failed. Request aborted. Help Reason given for failure: CSRF cookie not set.
我怎样才能解决这个问题?
编辑:以下是推荐更改后的输出:
使用csrf_exempt装饰您的视图.为此,您需要将"django.middleware.csrf.CsrfViewMiddleware"添加到MIDDLEWARE_CLASSES变量中.csrf_exempt基本上标记了您对免于任何CSRF检查的看法.更多细节在这里.
from django.views.decorators.csrf import csrf_exempt @csrf_exempt def index(request): # loop through keys for key in request.POST: value = request.POST[key] # loop through keys and values output ="" for key, value in request.POST.iteritems(): output= output + str(key) + " " + str(value) + "<br>" return HttpResponse(output)