在我提交具有图像字段的模型时,在Django上,保存模型时,在保存上传文件时会出现此错误
模型简单有一行
student(model):
photo = models.ImageField(upload_to='/photos')
提交表单时会出现此错误
OSError(13, 'Permission denied')
在模型save()方法中使用此点
/usr/lib/python2.7/site-packages/django/core/files/storage.py in _save
fd = os.open(full_path, flags, 0o666)
引发错误的部件代码是
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)
在Django源代码中检查此行: https://github.com/django/django/blob/master/django/core/files/storage.py#L221
变量内容
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'
您认为这是一个许可问题,但即使是:
chmod -R 777 *
我得到了同样的错误,所以这不是权限问题,而是其他限制
的问题/ var / www / pr5 / 是项目目录
用户和组是cherokee:cherokee用于其下的目录中的所有文件
chown -R cherokee:cherokee *
并且chmod也是777(应该是770)
我正在使用切诺基作为Web服务器来提供静态和媒体文件 使用此命令在套接字上运行
/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,
)
追踪
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'
我搜索了其他问题,但没有解决这个问题。
表单是表格有效的经典的简单保存实例
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)
答案 0 :(得分:0)
使用在tmp目录中创建的套接字执行导致问题
我通过移动套接字文件解决了这个问题:
从/tmp/djangoche.socket到/var/www/pr5/server.socket
WSGI和uWSGI配置案例出现问题
如果没有修复,你也可以尝试这个
(仅适用于cherokee服务器)取消选中Cherokee管理中的“检查文件”,以获取处理WSGI(或类似)的虚拟服务器,这适用于基于文件的脚本,如php,它会导致很多问题案件。 此选项位于常见CGI选项部分
Secondely(可选与第一个修复一起使用),添加到您的WSGI.py文件中:
导入系统 sys.path.append( '/路径/要/项目') sys.path.append( '/路径/到/项目/项目名称')