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)
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
(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
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.
Comments