Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save CleitonDeLima/377d9357541e5614c8cce3949776c5d4 to your computer and use it in GitHub Desktop.
Save CleitonDeLima/377d9357541e5614c8cce3949776c5d4 to your computer and use it in GitHub Desktop.
Fix Django Media Folder Permissions

Check your current file/directory permission with:

stat -c "%a" /path/to/dir_or_file

To recursively give directories read&execute privileges:

find /path/to/base/dir -type d -exec chmod 755 {} +

To recursively give files read privileges:

find /path/to/base/dir -type f -exec chmod 644 {} +

Or, if there are many objects to process:

chmod 755 $(find /path/to/base/dir -type d)
chmod 644 $(find /path/to/base/dir -type f)

Or, to reduce chmod spawning:

find /path/to/base/dir -type d -print0 | xargs -0 chmod 755 
find /path/to/base/dir -type f -print0 | xargs -0 chmod 644

Source:

You can create a more secure set of permissions if you know your environment and combine who owns the directories/files with chown and then who can do what with chmod.


In case you need to really ensure how Django will behave when creating directories and files:

settings.py

FILE_UPLOAD_DIRECTORY_PERMISSIONS = 0o755
FILE_UPLOAD_PERMISSIONS = 0o644

It is important that the values are in ocatal base. (prefix 0o).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment