Write permission nginx gunicorn

Viewed 76

In my django/python code I have the following lines:

f = open(os.path.join(DIRECTORY, filname), 'w')
f.write(filecontent)
f.close()

when I have DIRECTORY='/tmp/' it all works fine, the file.txt is saved with owner=root and group=www-data. However, when I want to save the file in a subdirectory of my django-project, for example DIRECTORY='/project/subfolder' the file doesn't appear, although I have that subfolder set to the same owner/group.

I suspect that it is a permission problem with nginx or gunicorn. Any suggestions?

I tried to solve my problem by just mounting the /tmp/ directory to the docker container where I use the file afterwards. But I ran into the problem, that files in the /tmp/ directory do not appear in docker, while as when I mount another folder like /project/subfolder/, these files do appear in docker. So either way, half works, but never both.

1 Answers

Did you check what kind of permissions your subfolder has?

I also tried saving some files in my project subdirectory. For example i had a "log" subfolder in my project folder and i had to change permissions using:

chown -R www-data:www-data /var/www/test/myproject/log



Update: Because of your relative path, django can't save it. You must specify the absolute path. Therefore in you settings create a variable:

BASE_DIR = Path(__file__).resolve().parent.parent

This is the absolute path location to you project. You have to import BASE_DIR to your py file and than you can use it like before:

f = open(os.path.join(BASE_DIR, filname), 'w')
f.write(filecontent)
f.close()
Related