0

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?

4
  • 1
    Have you checked the folder permissions? If not, set the folder permissions to allow file creation... Commented Jun 17, 2019 at 13:28
  • 1
    Since this is file system storage, / refers to the root directly of your filesystem. Are you sure that's where you want to store things? Surely MEDIA_ROOT would be a better place. Commented Jun 17, 2019 at 13:31
  • 1
    Check out the answer on this question: stackoverflow.com/questions/1682440/… Commented Jun 17, 2019 at 13:34
  • @DanielRoseman for some reason, I thought / refers to the root of the project that's currently running and I've changed those permissions. Modified the path now and it's fine.Thanks! Commented Jun 17, 2019 at 13:37

2 Answers 2

1

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/'.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.