Permission denied when saving a record with an ImageField in the model

567 views
Skip to first unread message

Sithembewena Lloyd Dube

unread,
Mar 17, 2012, 11:02:39 AM3/17/12
to django...@googlegroups.com
Hi all,

I have a model with an ImageField and when I try to save a record in the admin site, I get the following:

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/admin/myapp/course/add/

Django Version: 1.3.1
Python Version: 2.7.2
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'munchlearnapp']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in wrapper
  307.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view
  93.                     response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/cache.py" in _wrapped_view_func
  79.         response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/sites.py" in inner
  197.             return view(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapper
  28.             return bound_func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view
  93.                     response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in bound_func
  24.                 return func(self, *args2, **kwargs2)
File "/usr/local/lib/python2.7/dist-packages/django/db/transaction.py" in inner
  217.                 res = func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in add_view
  882.                 self.save_model(request, new_object, form, change=False)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in save_model
  665.         obj.save()
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save
  460.         self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save_base
  543.                         for f in meta.local_fields if not isinstance(f, AutoField)]
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/files.py" in pre_save
  255.             file.save(file.name, file, save=False)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/files.py" in save
  92.         self.name = self.storage.save(name, content)
File "/usr/local/lib/python2.7/dist-packages/django/core/files/storage.py" in save
  49.         name = self._save(name, content)
File "/usr/local/lib/python2.7/dist-packages/django/core/files/storage.py" in _save
  166.             os.makedirs(directory)
File "/usr/lib/python2.7/os.py" in makedirs
  150.             makedirs(head, mode)
File "/usr/lib/python2.7/os.py" in makedirs
  150.             makedirs(head, mode)
File "/usr/lib/python2.7/os.py" in makedirs
  150.             makedirs(head, mode)
File "/usr/lib/python2.7/os.py" in makedirs
  150.             makedirs(head, mode)
File "/usr/lib/python2.7/os.py" in makedirs
  157.     mkdir(name, mode)

Exception Type: OSError at /admin/myapp/course/add/
Exception Value: [Errno 13] Permission denied: '/home/projects'


I'm on Ubuntu 11.10, using Django 1.3.1. Any thoughts? Thanks.

--
Regards,
Sithembewena Lloyd Dube

Jonathan D. Baker

unread,
Mar 17, 2012, 12:13:12 PM3/17/12
to django...@googlegroups.com, django...@googlegroups.com
You need to make sure your server has  write permissions on the home/projects directory.

Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Sithembewena Lloyd Dube

unread,
Mar 17, 2012, 12:33:10 PM3/17/12
to django...@googlegroups.com
Hi Jonathan,

Thanks for the response. How do I do that? I ran sudo chmod -R a+rwX Projects/ and I still get the same error. How do I go about making the folder writable?

Thanks.

Jonathan Baker

unread,
Mar 17, 2012, 12:58:22 PM3/17/12
to django...@googlegroups.com
Sithembewena,

I wasn't exactly right in my response, but close. You need to make sure that the same process running your python interpreter has write permissions to your media directory.

From StackOverflow:
"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 chrown 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."
Jonathan D. Baker
Web Developer
http://jonathandbaker.com
303.257.4144

Sithembewena Lloyd Dube

unread,
Mar 17, 2012, 1:06:36 PM3/17/12
to django...@googlegroups.com
Thanks Jonathan,

I actualyl came across that answer but am stupmed. I checked man chown in my terminal and I never understand those manuals. I will try to find my python process id.

Jonathan Baker

unread,
Mar 17, 2012, 1:18:18 PM3/17/12
to django...@googlegroups.com
Chown changes the owner of a file. If you navigate to the directory giving you trouble using terminal and run 'ls -latr', you'll get a print out of both the owner and group for the directory contents. The first name listed is the owner, the second is the group. You need to ensure that the group that the owner of home/projects belongs to matches the group of your python process. You can find out the owner of your python process by running:
$ which python
...and then navigating to that directory, and also running ls -latr and checking the output on your terminal.

On my OS (also Ubuntu), the python process is owned by the root user who is a member of the root group. So, I would need to run this command to ensure that the owner of home/projects is a member of the same group (root):
$ sudo chown <username>:root /home/projects
...change username to your own system username.
Reply all
Reply to author
Forward
0 new messages