I'm trying to upload a file via a form inside a bootstrap modal.
request.POST correctly shows the file name in QueryDict, but request.FILES shows an empty MultiValueDict.
For comparison, I also tried another version of the form that is exposed in the page, which seems to be working perfectly.
What's the difference? Thanks.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<!-- EXPOSED FORM -->
<form action="." method='post' enctype="multipart/form-data">
{% csrf_token %}
{{ file_form.as_p }}
<input type="submit" value="Upload">
</form>
<!-- MODAL VERSION -->
<a href="#" type="button" class="btn" data-toggle="modal" data-target="#statusModal">
Modal
</a>
<div class="modal fade" id="statusModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">UPLOAD FILE</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form action =".?&status=status-update" enctype="multipart/form-data" method = "post">
<div class="modal-body">
{% csrf_token %}
{{ file_form.as_p }}
</div>
<div class="modal-footer">
<input class="btn green-button" type = "submit" value = "Send">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
views.py:
from django.shortcuts import render, redirect
from upload_file.forms import FileForm
from upload_file.models import File
def index(request):
if request.method == "POST":
print(request.POST)
print(request.FILES)
file_form = FileForm(request.POST, request.FILES)
if file_form.is_valid:
file_form.save()
return redirect('/upload_file/')
file_form = FileForm()
return render(request, 'upload_file/index.html', {
'file_form': file_form,
})
EDIT:
Adding here the rendered HTML:
<body>
<!-- UPLOAD FILE -->
<form action="." method='post' enctype="multipart/form-data">
<input type="hidden" name="csrfmiddlewaretoken" value="VOmIk8wKOSzjPQoxy2fh8mWlDIF2TK3ILEOXXSJcyfKxvMLR4bsJQLnUI02XNe4U">
<p><label for="id_name">Name:</label> <input type="text" name="name" value="default" maxlength="10" required id="id_name"></p>
<p><label for="id_file">File:</label> <input type="file" name="file" id="id_file"></p>
<input type="submit" value="Upload">
</form>
<!-- MODAL VERSION -->
<a href="#" type="button" class="btn" data-toggle="modal" data-target="#statusModal">
Modal
</a>
<div class="modal fade" id="statusModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">UPLOAD FILE</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form action =".?&status=status-update" enctype="multipart/form-data" method = "post">
<div class="modal-body">
<input type="hidden" name="csrfmiddlewaretoken" value="VOmIk8wKOSzjPQoxy2fh8mWlDIF2TK3ILEOXXSJcyfKxvMLR4bsJQLnUI02XNe4U">
<p><label for="id_name">Name:</label> <input type="text" name="name" value="default" maxlength="10" required id="id_name"></p>
<p><label for="id_file">File:</label> <input type="file" name="file" id="id_file"></p>
</div>
<div class="modal-footer">
<input class="btn green-button" type = "submit" value = "Send">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
</div>
</body>