OSError [Errno 13] Permission denied on file upload

Cherif KAOUA

On Django when i submit a modelform that has an image field, this error is raised when saving the model, when saving the uploaded file

The model simple have a line

student(model):
    photo = models.ImageField(upload_to='/photos')

This error is raised when submiting the form

OSError(13, 'Permission denied')

Occurs in the model save() method util this point

/usr/lib/python2.7/site-packages/django/core/files/storage.py in _save
                fd = os.open(full_path, flags, 0o666)

The part code that raise the error is

flags = (os.O_WRONLY | os.O_CREAT | os.O_EXCL |
                         getattr(os, 'O_BINARY', 0))
                # The current umask value is masked out by os.open!
fd = os.open(full_path, flags, 0o666)

Check this line in Django source code : https://github.com/django/django/blob/master/django/core/files/storage.py#L221

variables contents

e : OSError(13, 'Permission denied')
name : u'photos/10703514_652406348206730_7516458761930522613_n.jpg'
self : <django.core.files.storage.FileSystemStorage object at 0x3654550>
content : <ImageFieldFile: 10703514_652406348206730_7516458761930522613_n.jpg>
flags : 193
directory : u'/var/www/pr5/mediafolder/photos'
full_path : u'/var/www/pr5/mediafolder/photos/10703514_652406348206730_7516458761930522613_n.jpg'

You think that it's a permission problem but even with :

chmod -R 777 * 

i get the same error, so it's not a permission problem but something else limiting

/var/www/pr5/ is the project directory

User and Group are cherokee:cherokee for all files in directory under it

chown -R cherokee:cherokee *

and chmod is 777 (should be 770) for all too

I'm using cherokee as Web server for serving static and media files runing on a socket with this command

/usr/bin/python2 /var/www/pr5/manage.py runfcgi method=threaded socket=/tmp/djangoche.socket daemonize=False protocol=scgi 

settings.py

FILE_UPLOAD_PERMISSIONS = 770
FILE_UPLOAD_DIRECTORY_PERMISSIONS =770


MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR + '/mediafolder/'
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR + '/staticfolder/'
STATICFILES_DIRS = (
BASE_DIR + STATIC_URL,
)

Trace is

Environment:


Request Method: POST
Request URL: http://localhost/student/new

Django Version: 1.7
Python Version: 2.7.5
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'widget_tweaks',
 'school')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  22.                 return view_func(request, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/django/views/generic/base.py" in view
  69.             return self.dispatch(request, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
  87.         return handler(request, *args, **kwargs)
File "/var/www/pr5/school/views.py" in post
  313.             studentvar=stu.save()
File "/usr/lib/python2.7/site-packages/django/forms/models.py" in save
  457.                              construct=False)
File "/usr/lib/python2.7/site-packages/django/forms/models.py" in save_instance
  103.         instance.save()
File "/usr/lib/python2.7/site-packages/django/db/models/base.py" in save
  590.                        force_update=force_update, update_fields=update_fields)
File "/usr/lib/python2.7/site-packages/django/db/models/base.py" in save_base
  618.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/usr/lib/python2.7/site-packages/django/db/models/base.py" in _save_table
  699.             result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/usr/lib/python2.7/site-packages/django/db/models/base.py" in _do_insert
  732.                                using=using, raw=raw)
File "/usr/lib/python2.7/site-packages/django/db/models/manager.py" in manager_method
  92.                 return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/django/db/models/query.py" in _insert
  921.         return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  919.             for sql, params in self.as_sql():
File "/usr/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in as_sql
  877.                 for obj in self.query.objs
File "/usr/lib/python2.7/site-packages/django/db/models/fields/files.py" in pre_save
  301.             file.save(file.name, file, save=False)
File "/usr/lib/python2.7/site-packages/django/db/models/fields/files.py" in save
  89.         self.name = self.storage.save(name, content)
File "/usr/lib/python2.7/site-packages/django/core/files/storage.py" in save
  51.         name = self._save(name, content)
File "/usr/lib/python2.7/site-packages/django/core/files/storage.py" in _save
  222.                     fd = os.open(full_path, flags, 0o666)

Exception Type: OSError at /student/new
Exception Value: [Errno 13] Permission denied: '/var/www/pr5/mediafolder/10703514_652406348206730_7516458761930522613_n.jpg'

I've searched other questions but nothing solved this issue.

The form is a classic one simpy save instance on form valid

class NewStudent(View):

    student_form = modelform_factory(Student,exclude=())

    def get(self, request,pk=None):

        stu=self.student_form() 
        return render(request,'school/student_new.html',{'form1': stu)
    def post(self, request):

        stu=self.student_form(request.POST,request.FILES)

        if stu.is_valid() :
            studentvar=stu.save()

            return redirect(to='student_detail',pk=studentvar.pk)

        return render(request,'school/student_new.html',{'form1': stu)
Cherif KAOUA

Executing with a socket created on tmp directory is causing the issue

I fixed this problem by moving the socket file :

from /tmp/djangoche.socket to /var/www/pr5/server.socket

Problem occured with WSGI and uWSGI config cases

You can also try this in addition if not fixed

  1. (for cherokee server only) Uncheck the "Check file" in Cherokee adminstration for the virtual serveur who hande the WSGI (or like) , this is suited for file based scripts like php and it cause lot of problems our case. This option is under Handler tab in Common CGI Options section

  2. Secondely (optional is worked with the first fix) , add to your WSGI.py file :

    import sys sys.path.append('/path/to/project') sys.path.append('/path/to/project/projectname')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

OSError - Errno 13 Permission denied

From Dev

OSError: [Errno 13] Permission denied:

From Dev

Errno 13 Permission denied Django Upload File

From Dev

"OSError: [Errno 13] Permission denied" error

From Dev

pip upgrade OSError: [Errno:13] Permission Denied

From Dev

OSError at /blog/create [Errno 13] Permission denied: '/static' during image upload in Django

From Dev

try except error for "OSError: [Errno 13] Permission denied" and writing to log file

From Java

pip install failing with: OSError: [Errno 13] Permission denied on directory

From Dev

pip install produces OSError: [Errno 13] Permission denied:

From Dev

OSError: [Errno 13] Permission denied Python subprocess.call()

From Dev

OSError: [Errno 13] Permission denied while calling os.remove()

From Dev

OSError: [Errno 13] Permission denied when updating setuptools

From Dev

OSError: [Errno 13] Permission denied when installing django

From Dev

makedirs gives OSError: [Errno 13] Permission denied: '/pdf_files'

From Dev

OSError: [Errno 13] Permission denied Python subprocess.call()

From Dev

OSError: [Errno 13] Permission denied when updating setuptools

From Dev

Apache: Errno 13 file permission denied

From Dev

IOError: [Errno 13] Permission denied

From Dev

Errno13, Permission denied when trying to read file

From Dev

Adding a line to Windows hosts file: PermissionError: [Errno 13] Permission denied

From Dev

Raspberry [Errno 13] permission denied opening file txt

From Dev

OSError: [Errno 13] Permission denied: '/dev/ttyACM0' - using pyserial from Python to Arduino

From Dev

OSError: [Errno 13] Permission denied while setting up django-filer

From Dev

unable to install psycopg2 on Mac, OSError: Errno13 permission denied

From Dev

Python Heroku allow pushed .exe to run - OSError: [Errno 13] Permission denied

From Dev

Errno 13 Permission denied in django-wkhtmltopdf

From Dev

Errno 13 Permission denied using Gunicorn

From Dev

Paramiko Python: IOError: [Errno 13] Permission denied

From Dev

pip install - PermissionError: [Errno 13] Permission denied

Related Related

  1. 1

    OSError - Errno 13 Permission denied

  2. 2

    OSError: [Errno 13] Permission denied:

  3. 3

    Errno 13 Permission denied Django Upload File

  4. 4

    "OSError: [Errno 13] Permission denied" error

  5. 5

    pip upgrade OSError: [Errno:13] Permission Denied

  6. 6

    OSError at /blog/create [Errno 13] Permission denied: '/static' during image upload in Django

  7. 7

    try except error for "OSError: [Errno 13] Permission denied" and writing to log file

  8. 8

    pip install failing with: OSError: [Errno 13] Permission denied on directory

  9. 9

    pip install produces OSError: [Errno 13] Permission denied:

  10. 10

    OSError: [Errno 13] Permission denied Python subprocess.call()

  11. 11

    OSError: [Errno 13] Permission denied while calling os.remove()

  12. 12

    OSError: [Errno 13] Permission denied when updating setuptools

  13. 13

    OSError: [Errno 13] Permission denied when installing django

  14. 14

    makedirs gives OSError: [Errno 13] Permission denied: '/pdf_files'

  15. 15

    OSError: [Errno 13] Permission denied Python subprocess.call()

  16. 16

    OSError: [Errno 13] Permission denied when updating setuptools

  17. 17

    Apache: Errno 13 file permission denied

  18. 18

    IOError: [Errno 13] Permission denied

  19. 19

    Errno13, Permission denied when trying to read file

  20. 20

    Adding a line to Windows hosts file: PermissionError: [Errno 13] Permission denied

  21. 21

    Raspberry [Errno 13] permission denied opening file txt

  22. 22

    OSError: [Errno 13] Permission denied: '/dev/ttyACM0' - using pyserial from Python to Arduino

  23. 23

    OSError: [Errno 13] Permission denied while setting up django-filer

  24. 24

    unable to install psycopg2 on Mac, OSError: Errno13 permission denied

  25. 25

    Python Heroku allow pushed .exe to run - OSError: [Errno 13] Permission denied

  26. 26

    Errno 13 Permission denied in django-wkhtmltopdf

  27. 27

    Errno 13 Permission denied using Gunicorn

  28. 28

    Paramiko Python: IOError: [Errno 13] Permission denied

  29. 29

    pip install - PermissionError: [Errno 13] Permission denied

HotTag

Archive