Skip to content

Instantly share code, notes, and snippets.

@douglasmiranda
Last active May 16, 2023 10:33
Show Gist options
  • Save douglasmiranda/2153f15a6581ef676f6dee7ac5584874 to your computer and use it in GitHub Desktop.
Save douglasmiranda/2153f15a6581ef676f6dee7ac5584874 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).

@kubos777
Copy link

kubos777 commented Jul 3, 2019

Thank's, is cool.

@mughalmuneeb786
Copy link

this also solved my problem thanks , I installed django on Cpanel :)

@programmerizak
Copy link

Thanks for sharing

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