2017-07-28 72 views
1

我目前正在爲一個網站製作個人資料圖片功能,我開始使用DropzoneJS,但我在提交文件時遇到問題。圖片將落入該區域,但會出現一個「X」字樣,當我將鼠標懸停在圖片上時,可以看到MultiValueDictKeyError錯誤。當我單擊提交按鈕而沒有選擇文件時,會出現同樣的錯誤。所以我認爲問題在於該文件沒有被button提交?我怎麼做?與Django一起使用DropzoneJS時的MultiValueDictKeyError

HTML/JS:

<script type='text/javascript'> 

Dropzone.options.myDropzone = { 

    init : function() { 
     var submitButton = document.querySelector("#submitBtn") 
     myDropzone = this; 
     submitButton.addEventListener("click", function() { 
      myDropzone.processQueue(); 
     }); 
     this.on("addedfile", function() { 
      document.getElementById('submitBtn').style.visibility = "visible"; 
     }); 

     this.on('addedfile', function(){ 
      if (this.files[1]!=null){ 
       this.removeFile(this.files[0]); 
      } 
     }); 


    } 
}; 

Dropzone.options.myAwesomeDropzone = { 
    accept: function(file, done) { 
    console.log("uploaded"); 

    }, 
    init: function() { 
    this.on("addedfile", function() { 
     if (this.files[1]!=null){ 
     this.removeFile(this.files[0]); 
     //document.getElementById('submitBtn').style.visibility = "visible"; 
     } 
    }); 
    } 


}; 
</script> 
<!-- Modal --> 
<div id="picModal" class="modal"> 

    <!-- Modal content --> 
    <div class="modal-content"> 
    <span class="close"></span> 
    <form action="{% url 'profile_test' %}" method='POST' enctype="multipart/form-data" class="dropzone" id="my-dropzone">{% csrf_token %} 
     <button id='submitBtn' type='submit' style='visibility: hidden;'> Submit </button> 
     <input id='submit-all' type='file' name='uploaded_image'/> 
      {{form}} 
    </form> 
    </div> 

</div> 

</body> 

Here's a screenshot of the error

EDIT

我添加autoProcessQueue: false,myDropzone。當我將鼠標懸停在圖片上時,我沒有收到問題。相反,現在當我按下提交它只是帶我到MultiValueDictKeyError錯誤頁面

* EDIT 2 **

views.py 

def profile_test(request): 
    #going to use this view to display the profile page, and test alterations to it 
    form = ImageUploadForm(request.POST, request.FILES) 
    user = User.objects.get(id=request.user.id) 
    if request.method == "POST": 
     print 'valid' 
     user.userprofile.img = request.POST.get('uploaded_image', False) 
     user.userprofile.save() 
     return HttpResponseRedirect(reverse("profile_test")) 
    else: 
     print 'invalid' 
     form = ImageUploadForm() 

    return render(request, 'profile_test.html', {form:'form', user: 'user'}) 

我曾經有user.userprofile.img = request.FILES["uploaded_image"]但改變了它,它似乎陌事情做得更好。現在圖像將落入該區域,當我將鼠標懸停在該區域上時,我不會看到該錯誤。但現在,當我提交它dropzone消失和個人資料圖片不會改變。

+1

上傳路徑中是否有相同的文件(同名)?它沒有導致這個錯誤? – Jayground

+0

@Jayground不,沒有,但那不重要,它只是改變文件的名稱,如果我決定這樣做。我正在做更多的研究,並且遇到了這個問題(https://stackoverflow.com/questions/5895588/django-multivaluedictkeyerror-error-how-do-i-deal-with-it),所以我改變了我的觀點,它似乎幫助。現在圖片會進入dropzone,當我將鼠標懸停在圖片上時不會出現錯誤。這是好的,但現在當我按提交配置文件圖片的dropzone將消失,但配置文件圖片不會更新,沒有任何反應 – Amon

+0

@Jayground請檢查我的編輯 – Amon

回答

1

我期望img屬性是FileField。對?

user.userprofile.img = request.POST.get('uploaded_image', False) 

請試試這個。

user.userprofile.img = request.FILES.get('uploaded_image') 
# if you have multiple files to upload 
user.userprofile.img = request.FILES.getlist('uploaded_image') 
+0

這是一個'ImageField',不知道這是否有所作爲 – Amon

+0

Omg it工作!我實際上嘗試了你的建議,但只是增加了「假」的論點。哪個返回了'AttributeError:'bool'對象沒有屬性'instance''我很愚蠢,顯然它指的是'False'參數。我應該進一步閱讀它。謝謝soooo很多人,你很棒 – Amon

+1

我很高興聽到它有幫助。 :) – Jayground