I would like to upload a folder to my application but on the server side the request.FILES collection is always empty.
Also form.is_valid evaluates to false
The solution for this problem seems to be to add enctype="multipart/form-data" but I have already added it an it doesn't work. As described here SO TOPIC ABOUT THIS
Inside the browser I also get "This field is required" after pressing submit and yes I have choosen a directory.
Here my relevant code
model.py
# Create your models here.
class Datasets(models.Model):
dataset_name = models.CharField(max_length=100, blank=True, null=True)
description = models.CharField(max_length=1000, blank=True, null=True)
share_with = models.ManyToManyField(User)
uploaded_by = models.CharField(max_length=1000, blank=True, null=True)
upvotes = models.IntegerField(null=False, default=0)
downvotes = models.IntegerField(null=False, default=0)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
datafiles = models.FileField(blank=True, null=True)
form.py
class UploadDatasetForm(forms.Form):
dataset_name = forms.CharField(
max_length=1000,
widget=forms.TextInput(attrs={'class': 'form-control'}))
description = forms.CharField(
max_length=1000,
widget=forms.TextInput(attrs={'class': 'form-control'}))
share_with = forms.MultipleChoiceField(
choices=tuple(UserProfile.objects.values_list('id', 'full_name')),
widget=forms.CheckboxSelectMultiple,
)
datafiles = forms.FileField(widget=forms.ClearableFileInput(
attrs={
'multiple': True,
'webkitdirectory': True,
'directory': True,
}))
the template
{% block content %}
<h2>Register Here</h2>
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Register</button>
</form>
view.py
@login_required
def upload_dataset(request):
print(request.user)
form = UploadDatasetForm()
if request.method == "POST":
print('Receiving a post request')
form = UploadDatasetForm(request.POST, request.FILES)
print(request.FILES)
if form.is_valid():
print("The form is valid")
print(form.cleaned_data)
dataset_name = form.cleaned_data['dataset_name']
description = form.cleaned_data['description']
datafiles = form.cleaned_data['datafiles']
share_with = form.cleaned_data['share_with']
users_to_share_with = set()
print(datafiles)
instance = Datasets.objects.create(
uploaded_by=request.user,
dataset_name=dataset_name,
description=description,
datafiles = datafiles,
upvotes=0,
downvotes=0,
)
for i in share_with:
instance.share_with.add(i)
return redirect("public:data")
print("The Dataset has been uploaded")
context = {"form": form}
return render(request, "upload_dataset.html", context)
{% endblock %}
I receive the message Receiving a post request but it will not go inside the form.is_valid
The output after pressing the submit button is
System check identified no issues (0 silenced).
July 25, 2021 - 11:42:53
Django version 3.2.3, using settings 'platform_oliveoils.settings'
Starting development server at http://0.0.0.0:40/
Quit the server with CONTROL-C.
Max
Receiving a post request
<MultiValueDict: {}>