I'm trying to make a django form in which I want to upload a photo (Dropzone) and enter its title. I can't send the title field and the photo to my view at the same time. Could you please help me ? Request.files seems empty.
add.js
// DropzoneJs
let myDropzone = new Dropzone("#kt_dropzonejs_example_1", {
url: "add", // Set the url for your upload script location
paramName: "file", // The name that will be used to transfer the file
autoProcessQueue: false,
maxFiles: 1,
maxFilesize: 1000, // MB
acceptedFiles: 'image/*',
addRemoveLinks: true,
init: function () {
this.on("success", function (file, response) {
console.log(response);
});
},
headers: {
'X-CSRFToken': document.querySelector('[name=csrfmiddlewaretoken]').value
}
});
the "url" parameter seems obligatory and gives me problems but necessary with keenthemes metronic
add.htm
<form class="form" novalidate="novalidate" id="kt_add_video_form" data-kt-redirect-url="/" action="#" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<!--begin::titre photo group-->
<div class="row mb-6">
<!--begin::Label-->
<label class="col-lg-3 col-form-label required fw-semibold fs-6" for="{{ form.caption.id_for_label }}">Titre de la photo</label>
<!--end::Label-->
<!--begin::input-->
<div class="col-lg-9 fv-row">
{% render_field form.caption class="form-control form-control-lg form-control-solid mb-3 mb-lg-0" placeholder="Titre"%}
</div>
<!--end::input-->
</div>
<!--end::titre photo group-->
<!--begin::Dropzone-->
<div class="dropzone" id="kt_dropzonejs_example_1">
<!--begin::Message-->
<div class="dz-message needsclick">
<i class="ki-duotone ki-file-up fs-3x"><span class="path1"></span><span class="path2"></span></i>
<!--begin::Info-->
<div class="ms-4">
<h5 class="fs-6 fw-bold text-gray-500 mb-1">Faites glisser votre photo ici ou cliquer pour les charger.</h5>
<span class="fs-7 fw-semibold text-gray-500">Jusqu'a 1 Go</span>
</div>
<!--end::Info-->
</div>
</div>
<!--end::Dropzone-->
<button type="submit" id="kt_add_video_submit"class="btn btn-primary" data-kt-videos-modal-action="submit">
views.py
def post(self, request, **kwargs):
context = super().get_context_data(**kwargs)
form = forms.PhotoForm(user_flotte, request.POST)
if form.is_valid():
obj = form.save(commit=False)
file = request.FILES['file']
this code returns me "MultiValueDictKeyError at /photos/ajout 'file'
request.FILES seems empty.
Thanks