[[Errno 2]] No such file or directory: '/tmp/tmp1d93dhp7.upload.mp4' in my Django project
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')
urls.py:
urlpatterns = [
path('', include('homepage.urls')),#redirects to transcribe/,
path('transcribe/', include('transcribe.urls')),
path('human/', include('human.urls')),
path('admin/', admin.site.urls),
]+ static(
settings.MEDIA_URL, document_root=settings.MEDIA_ROOT
)
Dockerfile:
version: "3.9"
services:
web:
build: .
command: python /code/manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- 8000:8000
depends_on:
- db
environment:
- "DJANGO_SECRET_KEY=my_secret_key"
- "DJANGO_DEBUG=True"
db:
image: postgres:13
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- "POSTGRES_HOST_AUTH_METHOD=trust"
volumes:
postgres_data: