1

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>

Here's a screenshot of the error

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.

Amon
  • 2,725
  • 5
  • 30
  • 52
  • 1
    Is there same file(same name) in your upload path? Didn't it cause that error? – Jayground Jul 28 '17 at 03:02
  • @Jayground No there isn't but that wouldn't matter, it just changes the name of the file if I decide to do that. I was doing some more research and came across this question (https://stackoverflow.com/questions/5895588/django-multivaluedictkeyerror-error-how-do-i-deal-with-it) so I changed my views and it seemed to help. Now the picture will go into the dropzone, and there won't be an error when I hover over it. Which is good, but now when I press submit the profile pic the dropzone will disappear but the profile pic wont update, nothing happens – Amon Jul 28 '17 at 03:10
  • @Jayground Please check my edit – Amon Jul 28 '17 at 03:15

1 Answers1

2

I expect that img attribute is FileField. right?

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

please try this.

user.userprofile.img = request.FILES.get('uploaded_image')
# if you have multiple files to upload
user.userprofile.img = request.FILES.getlist('uploaded_image')
Jayground
  • 1,797
  • 1
  • 17
  • 32
  • It's an `ImageField`, not sure if that makes a difference – Amon Jul 28 '17 at 03:22
  • Omg it worked!!! I actually tried what you suggested but just with the added `False` argument. Which returned `AttributeError: 'bool' object has no attribute 'instance'` I'm so dumb, clearly it was referring to the `False` argument. I should've read further into it. Thanks soooo much man you're awesome – Amon Jul 28 '17 at 03:24
  • 1
    I am glad to hear that it was helpful. :) – Jayground Jul 28 '17 at 04:40