Answer the question
In order to leave comments, you need to log in
How to upload multiple Django files?
views.py
from django.views.generic.edit import FormView
from .forms import UploadForm
from .models import Attachment
class UploadView(FormView):
template_name = 'form.html'
form_class = UploadForm
success_url = '/done/'
def form_valid(self, form):
for each in form.cleaned_data['attachments']:
Attachment.objects.create(file=each)
return super(UploadView, self).form_valid(form)
from django.db import models
class Attachment(models.Model):
file = models.FileField(upload_to='')
from django import forms
from multiupload.fields import MultiFileField
class UploadForm(forms.Form):
attachments = MultiFileField(min_num=1)
{% extends 'mainpage/homepage.html' %}
{% load static %}
{% block content %}
<form enctype="multipart/form-data" method="post">
{% csrf_token %}
<input type="file" name="myfiles" multiple>
<button type="submit">Upload</button>
</form>
{% if uploaded_file_url %}
<p>File uploaded at: <a href="{{ uploaded_file_url }}">{{ uploaded_file_url }}</a></p>
{% endif %}
<p><a href="">Return to home</a></p>
{% endblock %}
Answer the question
In order to leave comments, you need to log in
SHADRIN here is a link to the documentation Uploading multiple files . Specifically, in your case, you need to change the form field as follows
attachments = models.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}))
<form enctype="multipart/form-data" method="post">
{% csrf_token %}
{{ form.attachments }}
<button type="submit">Upload</button>
</form>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question