I'm glad you decided that. I spent several hours on it, so I solved it:
The main problem with using dropzone is that as soon as the files are uploaded, it will start loading. Thus, images will not be loaded with the rest of the form data.
To handle this, I had to create a dropzone object programmatically with the following settings:
$(document).ready(function(){ var list_of_files = new Array(); Dropzone.autoDiscover = false; //prevent dropzone to automatically discover the dropzone object in your html $("div#dropzone").dropzone({ uploadMultiple: true, // allow multiple upload autoProcessQueue: false, // prevent dropzone from uploading automatically url: "/", //dropzone needs a url attribute or it complains, what value you put here does not really matter. It is only purpose is to prevent a javascript error message from chrome console maxFiles: 5, //set max uploads to 5 since you only have 5 image files in your model init: function(){ //everytime a file is uploaded, save the file object //for later use this.on("addedfile", function(file) { if (list_of_files.length < 5) { list_of_files.push(file) console.log("file added"); } }); } }); // the following function override the "submit" button in the form $(document).on("click", "button", function(e){ e.preventDefault() //prevent the form from submitting console.log('num of files: ' + list_of_files.length); var formData = new FormData(); // construct our own upload data var inputs = $("#newUserForm input"); //get all of the data from textboxes $.each(inputs, function(obj, v){ var name = $(v).attr("name") var val = $(v).val(); console.log('name: ' + name + ' value: ' + val); formData.append(name, val); }); //get the file object from dropzone and put it into our formdata for(i=0;i<list_of_files.length;i++) { formData.append('user_image'+(i+1), list_of_files[i]); } var request = new XMLHttpRequest(); request.open("POST", "/"); //config your post url here request.send(formData); //send the post request to server }); });
Here is the template file:
<form id="newUserForm" name="newUserForm" method="post" enctype="multipart/form-data"> {% csrf_token %} {% if form %} {% for field in form %} <p>{{ field.label_tag }} {{ field }}</p> {% endfor %} {% endif %} <div id="dropzone" class="dropzone"></div> <button id='save'> save </button> </form>
I also added an exception to forms.py (so that these fields do not appear in our template, we have dropzone to replace them):
class CustomerInfoForm(forms.ModelForm): class Meta: model = CustomerProfile exclude=('user_image1','user_image2','user_image3','user_image4','user_image5')
All of the code above is to send data from each text box with images of your .py views together in one step
Here are views.py :
def index(request): if request.method == 'POST': form = CustomerInfoForm(request.POST) if (form.is_valid()): instance = form.save(commit=False)
I tested this solution using Django 1.8 and it works. I checked the database and the path was correctly written to the record.
Now, to reflect on this decision, he defeated the goal of using dropzone. Since users can not upload photos immediately after selecting a file.
Since you also solved this problem. Please post your decision, and I look forward to learning something new from you :)