I'm currently making a profile picture feature for a website, I started using DropzoneJS but I'm having an issue submitting the file. The picture will drop into the zone fine, but it appears with an "X" over it, and when I hover over the picture I can see the MultiValueDictKeyError
error. Which is the same error I get when I click the submit button without selecting a file. So I assume the issue is that the file isn't being submitted by the button
? How do I do that?
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>
EDIT
I added autoProcessQueue: false,
to myDropzone
. I'm not getting the issue when I hover over the picture anymore. Instead now when I press submit it just takes me to the MultiValueDictKeyError
error page
*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'})
I used to have user.userprofile.img = request.FILES["uploaded_image"]
but changed it, it seemed to maek things better. Now the image will drop into the zone, and when I hover over it I won't see that error. But now when I submit it the dropzone disappears and the profile pic doesnt change.