ismail
ismail

Reputation: 3923

Permission Denied error with Django while uploading a file

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/'

Upvotes: 31

Views: 49359

Answers (6)

Ehsan88
Ehsan88

Reputation: 3781

This is a permission issue and you have to understand linux permissions. The other answers will probably help you with that. BUT one another important point. If you deploy the web app using Apache, the changes does not take effect unless you restart apache by: systemctl restart apache. So basically if you fix the ownership/permissio using your linux knowledge and restart apahce after that you can fix the problem.

Upvotes: 0

Taha Moeini
Taha Moeini

Reputation: 51

You had to modify the permissions and owner settings in order for your app to be able to upload media files. You should run these commands to grant permission to your Django app as the default group for Apache is www-data.

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

By doing so, a new user group called "www-data" is created, the user "www-data" is added to it, the user group of the media is changed to "www-data," and finally the owner privileges are changed to "770," granting read, write, and execute rights to the owner (root) and owner group (www-data), but denying access to anyone else. Now that www-data is a member of the www-data group, it has the ability to read and write.

Upvotes: 3

Moritz
Moritz

Reputation: 3363

Just in case you run into this when running your development server. I ran the development server as root like this: sudo python manage.py runserver 0.0.0.0:80 in order to test the site with an iPad in the same LAN network. The cache files generated in that session belonged to root. So when I ran the project the next day NOT as root I got the permission denied error.

Upvotes: 4

geekingreen
geekingreen

Reputation: 992

I just ran into this same problem. And found the solution if you are hosting with Apache as your server. For instance if my settings were:

MEDIA_ROOT = '/var/www/media/geekingreen'

then I would simply need to give that folder the correct permissions recursively to make sure that any sub-folders also have the same permission. The default group for apache is www-data so to give permission to my django app I would run these commands.

cd /var/www/media
chgrp -R www-data geekingreen/
chmod -R g+w geekingreen/

The chgrp -R www-data geekingreen/ command changes the directory geekingreen and any subdirectories to have the group www-data.
The chmod -R g+w geekingreen/ command changes what permissions the group has on all of these folders that now belong to www-data, to now have the write permission. Obviously required for uploads.

Hope this can help anyone that may have had a similar problem.

Upvotes: 80

John Mee
John Mee

Reputation: 52253

mkdir(name, mode)

Exception Type: OSError at /admin/products/photo/add/

but your application is deployed at

/var/www/django_projects/gangr/../gangr/

Do you have a directory path set to an absolute path "/admin/products/photo/add/" rather than something relative like "admin/products/photo/add/"?

Check the MEDIA_ROOT and MEDIA_URL in your settings.py file.

http://docs.djangoproject.com/en/dev/ref/settings/#media-root

Upvotes: 3

Peter Rowell
Peter Rowell

Reputation: 17713

Try checking the permissions on each directory in the path starting at /. Just a thought.

Upvotes: 13

Related Questions