2016-03-02 69 views
1

我已經在django表單中啓用了csrf標記,並且如果我嘗試上傳文件,則會獲取低於錯誤的錯誤。請幫我解決它。django csrf標記錯誤 - 禁止(CSRF cookie未設置)

故宮(CSRF的cookie未設置。):/上傳

我的HTML表單呈現如下圖所示。

<form id="uploadfile" action="/upload" class="dropzone needsclick dz-clickable" enctype="multipart/form-data" method="post" style="display: none;"> 
     <input type="hidden" name="csrfmiddlewaretoken" value="I4DEvg2nDPGkaGjrynMVGh5KfGdk3Z3z"> 
     <div class="dz-message needsclick" style="display: block;"> 
      Drop files here or click to upload.<br> 
     </div> 

    <input type="hidden" name="cmd" value="mycmd"></form> 

我的上傳視圖代碼如下所示。

def upload(request): 
    # handle form upload 
    if request.method == 'POST': 
     cmd = request.POST.get('cmd','') 
     form = Utils.Form() 
     upfile = form.uploadFile(request) 
     ....some code..... 
     return HttpResponse("my response") 

在settings.py我已啓用cookie和csrf像下面。

........ 
........ 
CSRF_COOKIE_SECURE = True 
CSRF_COOKIE_HTTPONLY = True 
SENDFILE_BACKEND = 'sendfile.backends.development' 
........ 
........ 
MIDDLEWARE_CLASSES = [ 
    'django.middleware.security.SecurityMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.common.CommonMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.middleware.clickjacking.XFrameOptionsMiddleware', 
] 
....... 
+1

我認爲,HTML是渲染的,並沒有你的模板。 – Viroide

+1

我的意思是,你有模板中的這個'{%csrf_token%}'嗎? – Viroide

+0

關閉當然我有 – virus

回答