Hi guys, as the title shows, I’m getting the error The submitted file is empty.. I’ve tried basically everything that I’ve found in this forum but nothing works. If I print out the request, this is what I’m getting: FILES: <MultiValueDict: {'file': [<InMemoryUploadedFile: untitled.glb (application/octet-stream)>]}> POST: <QueryDict: {'csrfmiddlewaretoken': ['XLsVQZRXD7Yf726r9vzeKEXze6cfAujP0KyD9xUhSWcB9wELwO5f6ubfiL7bJk4T'], 'name': ['Testa treno'], 'scala': ['1']}>
. I’ll attach my code here:
Model:
def model_file_path(instance, filename):
return f"models/{instance.id}/model.glb"
class ThreeDModel(models.Model):
# Generate a unique folder identifier for each model.
unique_folder = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
name = models.CharField(max_length=128)
# The GLB file will always be saved as 'model.glb' in the model's unique folder.
file = models.FileField(upload_to=model_file_path)
print(file)
# Optionally, add a thumbnail field that stores images inside an 'img' folder.
# thumbnail = models.ImageField(upload_to=threedmodel_thumbnail_upload_to, blank=True, null=True)
scala = models.FloatField(blank=False, null=False, default=1.0)
user = models.ForeignKey(
UserProfile,
on_delete=models.CASCADE,
verbose_name="User"
)
project = models.ForeignKey(
Project,
on_delete=models.CASCADE,
verbose_name="Project"
)
def __str__(self):
return self.name
View:
class ThreeDModelCreateView(LoginRequiredMixin, CreateView):
model = ThreeDModel
form_class = ThreeDModelForm
template_name = 'model/threedmodel_create.html'
def dispatch(self, request, *args, **kwargs):
self.project = get_object_or_404(
Project,
pk=self.kwargs['project_pk'],
azienda=request.user.profile.azienda
)
return super().dispatch(request, *args, **kwargs)
def form_valid(self, form):
# Save the instance with commit=False to set project/user
obj = form.save(commit=False)
obj.project = self.project
obj.user = self.request.user.profile
# Save the instance to generate an ID
obj.save()
# Now save the file using the FileField's save method
file = self.request.FILES['file']
obj.file.save(file.name, file, save=True)
return super().form_valid(form)
def get_success_url(self):
return reverse_lazy('project_dashboard', kwargs={'pk': self.project.pk})
Form
class ThreeDModelForm(forms.ModelForm):
class Meta:
model = ThreeDModel
fields = ['name', 'file', 'scala']
def __init__(self, *args, **kwargs):
self.project = kwargs.pop('project', None)
self.user = kwargs.pop('user', None)
super().__init__(*args, **kwargs)
def save(self, commit=True):
instance = super().save(commit=False)
instance.project = self.project
instance.user = self.user
if commit:
instance.save()
return instance
HTML:
{% extends "base.html" %}
{% block title %}Create 3D Model{% endblock %}
{% block content %}
<div class="container mt-4">
<h2>Create New 3D Model for project: {{ project.name }}</h2>
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="btn btn-primary">Create 3D Model</button>
</form>
</div>
{% endblock %}
EDIT 1:
I’ve discovered that the files are uploaded to the media folder rather then the static folder (and pushed into the database), for some reason which is in line with what the DjangoDocs says about the “The submitted file is empty.”:
newforms validation error comes up because there is an inconsistency in UploadedFile and InMemoryUploadedFile argument order.
Compare lines 23, 142, and 143 of django/django/core/files/uploadedfile.py at main · django/django · GitHub
Little note: the file that are being saved and are not returning the error are PDF and other random files, but when I upload a glb file, the error comes up.
This is my settings.py root and url section:
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_URL = '/static/'