Permission Denied error with Django while uploading a file

Viewed 45146

I currently have a simple model defined, with a photoupload feature using django thumbnails plugin.

but when i try to upload it gives me the following error:

OSError at /admin/products/photo/add/

(13, 'Permission denied')

Now, i know this is seems to be a permission issue, so the first thing i checked were permissions on the directory and changed these to 777 (Just to Test), restarted the server and fcgi and it still gives the error.

Traceback

Traceback: File "/usr/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
  92.                 response = callback(request, *callback_args,
**callback_kwargs) File "/usr/lib/python2.6/dist-packages/django/contrib/admin/options.py" in wrapper
  226.                 return self.admin_site.admin_view(view)(*args,
**kwargs) File "/usr/lib/python2.6/dist-packages/django/views/decorators/cache.py" in _wrapped_view_func
  44.         response = view_func(request, *args, **kwargs) File "/usr/lib/python2.6/dist-packages/django/contrib/admin/sites.py" in inner
  186.             return view(request, *args, **kwargs) File "/usr/lib/python2.6/dist-packages/django/db/transaction.py" in _commit_on_success
  240.                 res = func(*args, **kw) File "/usr/lib/python2.6/dist-packages/django/contrib/admin/options.py" in add_view
  734.                 self.save_model(request, new_object, form, change=False) File "/usr/lib/python2.6/dist-packages/django/contrib/admin/options.py" in save_model
  557.         obj.save() File "/usr/lib/python2.6/dist-packages/django/db/models/base.py" in save
  410.         self.save_base(force_insert=force_insert, force_update=force_update) File "/usr/lib/python2.6/dist-packages/django/db/models/base.py" in save_base
  483.                     values = [(f, f.get_db_prep_save(raw and getattr(self, f.attname) or f.pre_save(self, True))) for f in meta.local_fields if not isinstance(f, AutoField)] File "/usr/lib/python2.6/dist-packages/django/db/models/fields/files.py" in pre_save
  252.             file.save(file.name, file, save=False) File "/var/www/django_projects/gang/../gang/products/thumbs.py" in save
  84.         super(ImageWithThumbsFieldFile, self).save(name, content, save) File "/usr/lib/python2.6/dist-packages/django/db/models/fields/files.py" in save
  91.         self.name = self.storage.save(name, content) File "/usr/lib/python2.6/dist-packages/django/core/files/storage.py" in save
  47.         name = self._save(name, content) File "/usr/lib/python2.6/dist-packages/django/core/files/storage.py" in _save
  146.             os.makedirs(directory) File "/usr/lib/python2.6/os.py" in makedirs
  150.             makedirs(head, mode) File "/usr/lib/python2.6/os.py" in makedirs
  150.             makedirs(head, mode) File "/usr/lib/python2.6/os.py" in makedirs
  150.             makedirs(head, mode) File "/usr/lib/python2.6/os.py" in makedirs
  157.     mkdir(name, mode)

Exception Type: OSError at /admin/products/photo/add/ Exception Value: (13, 'Permission denied')

The user that the FCGI daemon is being run on definitely has access to read and write to that directory.

From settings.py

MEDIA_ROOT = '/var/www/sites/gang/http/media/'
MEDIA_ROOT_URL = '/media/'
5 Answers

In order for your app to be able upload media files, you needed to change the permissions and owner settings. The default group for apache is www-data so to give permission to your Django app you should run these commands.

sudo groupadd www-data
sudo adduser www-data www-data
sudo chgrp -R www-data media
sudo chown -R www-data media
sudo chmod -R 770 media

Step-by-step, this creates a new user group called 'www-data', adds the user www-data to that group, changes the user group of media to 'www-data' and finally changes the owner privileges to 770 which allows read, write and, execute rights to the owner (root) and owner group (www-data) with no access rights to anyone else. Because www-data was added to the www-data group, www-data can now read and write.

Related