Django File upload looping

161 Views Asked by At

How would I best loop a form like this (there is other code in the form but this is an example for image uploading)

<form action="{% url "upload" %}" method="post" enctype="multipart/form-data">
  {% csrf_token %}
  <input type="file" name="image_file">
  <input type="file" name="image_file2">
  <input type="file" name="image_file3">
  <input type="file" name="image_file4">
  <input type="file" name="image_file5">
  <input type="submit" value="submit" />
</form>

for single file uploading but need it multifile uploading:

def image_upload(request):
    if request.method == "POST" and request.FILES["image_file"]:
        image_file = request.FILES["image_file"]
        fs = FileSystemStorage()
        filename = fs.save(image_file.name, image_file)
        image_url = fs.url(filename)
        print(image_url)
        return render(request, "upload.html", {
            "image_url": image_url
        })
    return render(request, "upload.html")

Just not sure how to loop it so images are stored along with filename in a var for the db (at a later point) also just a thought, might not always be uploading all five files could be three or none

0

There are 0 best solutions below