title: OSError [Errno 13] Permission denied on file upload
body: <p>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  </p>
  
  <p>The model simple have a line </p>
  
  <pre><code>student(model):
      photo = models.ImageField(upload_to='/photos')
  </code></pre>
  
  <p>This error is raised when submiting the form</p>
  
  <pre><code>OSError(13, 'Permission denied')
  </code></pre>
  
  <p>Occurs in the model save() method util this point </p>
  
  <pre><code>/usr/lib/python2.7/site-packages/django/core/files/storage.py in _save
                  fd = os.open(full_path, flags, 0o666)
  </code></pre>
  
  <p>The part code that raise the error is </p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Check this line in Django source code :
  <a href="https://github.com/django/django/blob/master/django/core/files/storage.py#L221" rel="nofollow">https://github.com/django/django/blob/master/django/core/files/storage.py#L221</a></p>
  
  <p>variables contents</p>
  
  <pre><code>e : OSError(13, 'Permission denied')
  name : u'photos/10703514_652406348206730_7516458761930522613_n.jpg'
  self : &lt;django.core.files.storage.FileSystemStorage object at 0x3654550&gt;
  content : &lt;ImageFieldFile: 10703514_652406348206730_7516458761930522613_n.jpg&gt;
  flags : 193
  directory : u'/var/www/pr5/mediafolder/photos'
  full_path : u'/var/www/pr5/mediafolder/photos/10703514_652406348206730_7516458761930522613_n.jpg'
  </code></pre>
  
  <p>You think that it's a permission problem but even with :</p>
  
  <pre><code>chmod -R 777 * 
  </code></pre>
  
  <p>i get the same error, so it's not a permission problem but something else limiting</p>
  
  <p><strong>/var/www/pr5/</strong> is the project directory</p>
  
  <p>User and Group are cherokee:cherokee for all files in directory under it</p>
  
  <pre><code>chown -R cherokee:cherokee *
  </code></pre>
  
  <p>and chmod is 777 (should be 770) for all too</p>
  
  <p>I'm using cherokee as Web server for serving static and media files
  runing on a socket with this command</p>
  
  <pre><code>/usr/bin/python2 /var/www/pr5/manage.py runfcgi method=threaded socket=/tmp/djangoche.socket daemonize=False protocol=scgi 
  </code></pre>
  
  <p>settings.py</p>
  
  <pre><code>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,
  )
  </code></pre>
  
  <p>Trace is</p>
  
  <pre><code>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'
  </code></pre>
  
  <p>I've searched other questions but nothing solved this issue.</p>
  
  <p>The form is a classic one simpy save instance on form valid</p>
  
  <pre><code>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)
  </code></pre>