Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - Media upload [Errno 13] Permission denied

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'
like image 415
Hanpan Avatar asked Apr 04 '11 19:04

Hanpan


4 Answers

I was getting the same error and fix it by changing:

MEDIA_ROOT = '/media/'

to:

MEDIA_ROOT = 'media/'

Under settings.py.

like image 184
tibuurcio Avatar answered Oct 13 '22 09:10

tibuurcio


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.

like image 38
bradley.ayers Avatar answered Oct 13 '22 10:10

bradley.ayers


For me, I forgot to add: MEDIA_ROOT = os.path.join(BASE_DIR,'media') to my settings.py file on my production server.

like image 1
Clement Tong Avatar answered Oct 13 '22 09:10

Clement Tong


Make sure you have done following

Your settings.py

...
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
...

Also set permission to media directory

chgrp -R www-data /path/to/media/
chmod -R g+w  /path/to/media/
like image 1
bikram Avatar answered Oct 13 '22 11:10

bikram