I was developing my website, just when I realized that the update_supplier view stopped working correctly. In this view I update the name and the image. For some reason it is only getting the name and not the image from the request. When I try to print request.FILES it returns a empty multidictionary.
<MultiValueDict: {}>
This is my form:
{% for supplier in all_suppliers %}<div class="modal fade" id="staticSupplierUpdate{{ supplier.id }}" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><h1 class="modal-title fs-5" id="staticBackdropLabel">Editar Fornecedor</h1><button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button></div><div class="modal-body"><form id="update_supplier_form" method="post" action="{% url 'update-supplier' supplier.id %}" enctype="multipart/form-data"> {% csrf_token %}<div class="form-group"><label class="label_form_create_project d-flex text-left" for="update_supplier_name{{ supplier.id }}">Nome:</label><input type="text" id="update_supplier_name{{ supplier.id }}" name="update_supplier_name" value="{{ supplier.name_supplier }}" required></div><div class="form-group"><label class="label_form_create_project d-flex text-left" for="update_supplier_image{{ supplier.pk }}">Imagem do fornecedor:</label><div class="custom-file"><input type="file" id="update_supplier_image{{ supplier.pk }}" class="custom-file-input" name="update_supplier_image" accept=".png, .jpg, .jpeg" onchange="previewImageSupplierUpdate(this, '{{ supplier.pk }}')"><label class="custom-file-label custom-file-label-update-supplier" for="update_supplier_image{{ supplier.pk }}">Escolha uma imagem</label></div><img id="imagePreview{{ supplier.pk }}" src="{{ supplier.image_supplier.url }}" alt="Image Preview" style="max-width: 200px; max-height: 200px; margin-top: 10px; "></div><button type="submit" class="btn btn-primary save_project">Salvar</button></form><form id="deleteSupplierForm" action="{% url 'delete-supplier' 0 %}" method="post" style="display: inline;"> {% csrf_token %}<input type="hidden" id="deleteSupplierId" name="delete_supplier_id" value=""><button type="submit" class="btn btn-primary delete_product mt-3" onclick="return confirmDeleteSupplier({{ supplier.id }})">Eliminar</button></form></div></div></div></div> {% endfor %}This is my view:
def update_supplier(request, supplier_id): supplier = get_object_or_404(Supplier, id=supplier_id) if request.method == 'POST': updated_supplier_name = request.POST.get('update_supplier_name') updated_supplier_image = request.FILES.get('update_supplier_image') if updated_supplier_image: supplier.image_supplier = updated_supplier_image supplier.name_supplier = updated_supplier_name supplier.save() return JsonResponse({'success': 'Informações atualizadas com sucesso'}) else: return JsonResponse({'error': 'Volte a preencher os campos.'})I tried looking the answer in chatGPT and in stackOverflow, but couldn't find it. The only thing they said is that I need to have a form with enctype="multipart/form-data" which I already have.