2014-01-10 48 views
2

enter image description hereDjango的HTTP

我使用的是鍍鉻郵遞員擴展來測試Django的請求和響應的功能,我將需要POST數據到一個Django應用程序POST錯誤。我的應用程序的看法是:

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) 

當我發送請求我得到:

Forbidden (403) 
CSRF verification failed. Request aborted. 
Help 
Reason given for failure: 
    CSRF cookie not set. 

我該如何解決這個問題?

編輯:這是你的建議更改後的輸出:

enter image description here

回答

7

裝飾你的觀點與csrf_exempt。爲此,您需要將'django.middleware.csrf.CsrfViewMiddleware'添加到MIDDLEWARE_CLASSES變量中。 csrf_exempt基本上標誌着你被豁免任何CSRF檢查的觀點。更多詳細信息here.

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) 
+0

謝謝praveen,你懂了!請參閱上面的更新。 – user61629

+0

謝謝。我無法在其他地方找到解決方案 – Ryan