Reputation: 2135
I'm working on a Django app (DRF), but I'm a JS engineer so this is all new to me.
I have this in my models.py, outside of any actual models:
storage = S3Storage(aws_s3_bucket_name=settings.DOCUMENTS_BUCKET)
upload_location = 'recordings/'
and this is a field it is used in, inside one of the models:
zip_file = models.FileField(
upload_to=upload_location, storage=storage, null=True)
This works OK in production. However, I want to be able to test it locally, add those zip files when developing locally. So I've added this for storage
and upload_location
:
if settings.DEBUG:
storage = FileSystemStorage(location='/')
upload_location = ''
Then, when I try saving a file from admin on localhost, I get the following error:
[Errno 13] Permission denied: '/my-file.zip'
If I got it right, the app cannot just create that location somewhere on my file system. Maybe I'm wrong. How can I solve this?
Upvotes: 0
Views: 1276
Reputation: 8103
Locations passed into FileField
are relative to the MEDIA_ROOT
unless it starts with a slash. If it starts with a slash, this is an absolute path and is relative to the root of your drive.
So, location='/'
means the root of your hard drive (hence the permissions issues.)
You can use ./
to use the MEDIA_ROOT
, but it's normally best to group your assets based on some criteria. For example, for a model called SomeModel
, place the assets somewhere like location='some-models/'
.
Upvotes: 1
Reputation: 765
I would recommend that you take a look at django-storages.
You can save files on models easily using FileSystem for localhost and S3 for production.
All configurations are gonna be on settings, so you wouldn't need to put IFs anymore.
Upvotes: 1