[Errno 2] No such file or directory: '/tmp/tmp1d93dhp7.upload.mp4'

I’ve been moving development of my website over to using Docker. I recently adjusted the location of media files when I was presented with this error: [Errno 2] No such file or directory: '/tmp/tmp1d93dhp7.upload.mp4' in Django. So far I’ve checked for typos in my file location code in my settings, views and models.

The website works by simply storing user uploaded media files in a media folder. Uploaded files are stored in media/human.

Here is the relevant code:

views.py:

...
                fs = FileSystemStorage()
                filename = fs.save(uploaded_file.name, uploaded_file)
                uploaded_file_path = fs.path(filename)
                file_type = mimetypes.guess_type(uploaded_file_path)

                request.session['uploaded_file_path'] = uploaded_file_path
                
                
                user_doc, created = RequirementsChat.objects.get_or_create(id=user_id)
                

            
                files = request.FILES.getlist('file')
                for file in files:
                    
                    user_doc, created = RequirementsChat.objects.get_or_create(id=user_id)
                    uploaded_file = UploadedFile(input_file=file, requirements_chat=user_doc, chat_id = user_id)
                    uploaded_file.save()
                user_doc.save()
...

The error seems to be caused by the uploaded_file.save() line.

models.py:

class RequirementsChat(models.Model):
    id = models.CharField(primary_key=True, max_length=40)
    alias = models.CharField(max_length=20, blank=True, null=True)
    email = models.CharField(max_length=100, blank=True, null=True)
    language = models.CharField(max_length=10, blank=True, null=True)
    due_date = models.CharField(max_length=10, blank=True, null=True)
    subtitle_type = models.CharField(max_length=10, blank=True, null=True)
    transcript_file_type = models.CharField(max_length=10, blank=True, null=True)
    additional_requirements = models.TextField(max_length=500, blank=True, null=True)
    date = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    url = models.CharField(max_length=250, blank=True, null=True)
    task_completed = models.BooleanField(default=False)
    
class UploadedFile(models.Model):
    input_file = models.FileField(upload_to='human_upload/')#new
    chat_id = models.CharField(max_length=40, null= True)
    requirements_chat = models.ForeignKey(RequirementsChat, on_delete=models.CASCADE, related_name='uploaded_files', null=True)

settings.py:

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": "db1",
        "USER": "postgres",
        "PASSWORD": "postgres",
        "HOST": "db", # set in docker-compose.yml
        "PORT": 5432, # default postgres port
    }
}   

MEDIA_URL='/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

If you’re running this in Docker, you want to ensure that your media directory is in an external volume or directory mount. (You don’t want to try and save files in the container.) Also, you need to ensure that the /tmp directory exists in an accessible location. (This specific error appears to have been caused by you not having a /tmp directory in that container.)