Class below is used to create a form in Django and get neccesary information. Problem is, it offers only one file to upload. I need to upload multiple files. I use Crispy forms.
My simplified view.py looks like:
class PostCreateView(LoginRequiredMixin, CreateView):
model = Post
fields = ['title', 'file_1']
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
HTML code:
{% extends "blog/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<div class="content-section">
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4">Fill form</legend>
{{ form|crispy }}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Submit</button>
</div>
</form>
</div>
{% endblock content %}
When I inspect page, object looks like:
<input type="file" name="file_1" class="clearablefileinput form-control-file" id="id_file_1">
I want it to contain multiple atribute. How can I achive that? I can't get it to work using documentation (https://docs.djangoproject.com/en/3.2/topics/http/file-uploads/).
I have tried:
widgets = {'file_1': form.ClearableFileInput(attrs={'multiple': True})}
form.instance.file_1 = form.FileField(widget=form.ClearableFileInput(attrs={'multiple':True}))
form.instance.file_1 = form.FileField(widget=form.FileInput(attrs={'multiple': True}))
My models.py
file_1 = models.FileField(blank=True, upload_to='PN_files/%Y/%m/%d/', verbose_name="File 1", validators=[validate_file_size], help_text="Allowed size is 50MB")
I can't find an example how to implement multiple files upload which could be implemented in my class.
UPDATE! (thanks to 'amadou sow') I've updated my class to:
class PostCreateView(LoginRequiredMixin, CreateView):
model = Post
fields = ['title', 'file_1']
def form_valid(self, form):
form.instance.author = self.request.user
obj = form.save(commit=False)
if self.request.FILES:
for f in self.request.FILES.getlist('file_1'):
obj = self.model.objects.create(file=f)
return super().form_valid(form)
And I've added script to my HTML page to add atribute of MULTIPLE:
<script>
$(document).ready(function(){
$('#id_file_1').attr("multiple","true");
})
</script>
Now I get an option to select multiple files and upload them, but when I do that, only 1 file is stored in my media folder.