我收到一条消息
[Errno 13] Permission denied: u'/home/.../...jpg' 当我尝试在 django 中上传文件时。但是只有当我尝试使用 localhost/ 时才会出现问题。当我使用 localhost:8000 运行或调试它时,我没有遇到任何问题。
当我运行脚本来设置我的数据库时,我使用函数创建文件夹。
我的代码:
def handle_uploaded_file(request,f):
user=Users.objects.get(id_u=request.user.id)
url=settings.MEDIA_URL+'images/'+user.mail+'/gallery/'+f.name
fullurl=settings.MEDIA_ROOT+'images/'+user.mail+'/gallery/'+f.name
#comprobar si existe el archivo
if not os.path.exists(fullurl):
destination = open(fullurl, 'wb+')
...
destination.close()
文件夹媒体中的权限。我想将文件上传到“图像”文件夹中。这里我看到其他人没有写权限,但是up文件夹有777。那么,为什么我的脚本在创建子文件夹时会更改权限?
4 drwxrwxrwx 3 bernardo www-data 4096 ago 3 09:42 .
4 drwxrwxr-x 11 bernardo www-data 4096 jul 31 12:36 ..
4 drwxrwxr-x 5 bernardo bernardo 4096 ago 3 09:42 images
我的 http.config
ServerName localhost
WSGIPythonPath /home/bernardo/workspace/mbosoziales
Alias /media/ /home/bernardo/workspace/mbosoziales/media/
Alias /static/ /home/bernardo/workspace/mbosoziales/static/
<Directory /home/bernardo/workspace/mbosoziales/static>
Order deny,allow
Allow from all
</Directory>
<Directory /home/bernardo/workspace/mbosoziales/media>
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias / /home/bernardo/workspace/mbosoziales/mbosoziales/wsgi.py
<Directory /home/bernardo/workspace/mbosoziales/mbosoziales>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
因此,每次运行为用户创建文件夹的第一个脚本后,我都必须更改权限,因为尽管我认为权限是正确的,但他们没有写入媒体文件夹的权限。
sudo chgrp -R www-data images/
chmod -R o+w images/
任何帮助,将不胜感激。对不起我的英语:)
添加了完整的例外:环境:
Request Method: POST
Request URL: localhost:8000/user/upload/
Django Version: 1.4
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'admin',
'login',
'suchen',
'user',
'photologue')
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/auth/decorators.py" in _wrapped_view
20. return view_func(request, *args, **kwargs)
File "/home/bernardo/workspace/mbosoziales/user/views.py" in upload
332. handle_uploaded_file(request,request.FILES['file'])
File "/home/bernardo/workspace/mbosoziales/user/views.py" in handle_uploaded_file
344. destination = open(fullurl, 'wb+')
Exception Type: IOError at /user/upload/
Exception Value: [Errno 13] Permission denied: u'/home/bernardo/workspace /mbosoziales /media/images/bern@ardo.com/gallery/list.csv'