Django - Media upload [Errno 13] Permission denied
Solution 1
The process that is running your Python interpreter doesn't have permission to write into the media directory. You'll need to either chgrp
or chown
the media directory to the same group as your Python process, and ensure you have at least g+rwx
on directories and g+rw
on files.
Solution 2
I was getting the same error and fix it by changing:
MEDIA_ROOT = '/media/'
to:
MEDIA_ROOT = 'media/'
Under settings.py
.
Solution 3
For me, I forgot to add: MEDIA_ROOT = os.path.join(BASE_DIR,'media') to my settings.py file on my production server.
Related videos on Youtube
Hanpan
Updated on July 09, 2022Comments
-
Hanpan almost 2 years
I'm having some trouble getting django to play nice with image uploads. My script will create directories based on the date like so:
file = models.FileField(upload_to='uploads/%m-%Y/')
Now, if I create the dated directory in the uploads folder and chmod the folder to 755, the upload works fine, but if I try to chmod the uploads folder without creating the dated sub folder (which I need django to do), I am getting a permissions error.
How do I make it so a folder will allow the creation of sub folders?
Here is a traceback:
Django Version: 1.3 Python Version: 2.5.2 Installed Applications: ['django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.admin', 'tagging', 'mediamanager', 'livesettings', 'projects'] Installed Middleware: ('django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware') Traceback: File "/var/lib/python-support/python2.5/django/core/handlers/base.py" in get_response 111. response = callback(request, *callback_args, **callback_kwargs) File "/var/lib/python-support/python2.5/django/utils/decorators.py" in _wrapped_view 93. response = view_func(request, *args, **kwargs) File "/var/lib/python-support/python2.5/django/contrib/auth/decorators.py" in _wrapped_view 23. return view_func(request, *args, **kwargs) File "/var/www/enigma-dev/enigma/mediamanager/views.py" in upload_media 24. m.upload_media(data=form.cleaned_data, params=params) File "/var/www/enigma-dev/enigma/mediamanager/models.py" in upload_media 63. self.save() File "/var/lib/python-support/python2.5/django/db/models/base.py" in save 460. self.save_base(using=using, force_insert=force_insert, force_update=force_update) File "/var/lib/python-support/python2.5/django/db/models/base.py" in save_base 543. for f in meta.local_fields if not isinstance(f, AutoField)] File "/var/lib/python-support/python2.5/django/db/models/fields/files.py" in pre_save 255. file.save(file.name, file, save=False) File "/var/lib/python-support/python2.5/django/db/models/fields/files.py" in save 92. self.name = self.storage.save(name, content) File "/var/lib/python-support/python2.5/django/core/files/storage.py" in save 49. name = self._save(name, content) File "/var/lib/python-support/python2.5/django/core/files/storage.py" in _save 166. os.makedirs(directory) File "/usr/lib/python2.5/os.py" in makedirs 171. mkdir(name, mode) Exception Type: OSError at /media-manager/upload/ Exception Value: [Errno 13] Permission denied: '/var/www/site-dev/site/static/uploads/04-2011'
-
Hanpan about 13 yearsSorry, could you tell me what command I would use to find out the permissions on a folder? I am using ubuntu.
-
Nick Merrill almost 11 yearsAnd how can you tell which group the Python process is on? Thanks!
-
chhantyal over 9 yearsIf you are running nginx, usually it's www-data. So
chown www-data:www-data -R media
command would work. It seems you are missing -R(recursive) option when giving permission. -
dzierzak about 4 years@chhantyal Thank you guy, you save my whole day ;)