在Django上传文件时出现OSError

时间:2010-06-29 00:35:38

标签: python django permissions file-upload file-permissions

在尝试发送带有图像字段的表单时,我得到了:
Exception Type: OSError at /user/register/
Exception Value: (13, 'Permission denied')

当然,我检查的第一件事是我的文件夹的权限,以防万一从'/'在整个路径上将它们设置为777。依然没有。所以我尝试在设置中添加参数,现在设置如下:

ADMIN_MEDIA_PREFIX  
'/site_media/admin/'
CACHE_BACKEND   
'locmem://'
DEFAULT_CHARSET     
'utf-8'
DEFAULT_CONTENT_TYPE    
'text/html'
DEFAULT_FILE_STORAGE    
'django.core.files.storage.FileSystemStorage'
FILE_CHARSET    
'utf-8'
FILE_UPLOAD_HANDLERS    
('django.core.files.uploadhandler.MemoryFileUploadHandler', 'django.core.files.uploadhandler.TemporaryFileUploadHandler')
FILE_UPLOAD_MAX_MEMORY_SIZE     
2621440
FILE_UPLOAD_PERMISSIONS     
777
FILE_UPLOAD_TEMP_DIR    
None
FS_ROOT     
'/home/rails/fandrive'
MEDIA_ROOT  
'/home/fandrive/www/fandrive/site_media'
MEDIA_URL   
'/site_media/'
PROJECT_PATH    
'/home/rails/fandrive'
SESSION_FILE_PATH   
None

Request.META:

CONTENT_LENGTH      
'8249'
CONTENT_TYPE    
'multipart/form-data; boundary=---------------------------26681719213985'
DOCUMENT_ROOT   
'/home/rails/fandrive/public'
HTTP_ACCEPT     
'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
HTTP_ACCEPT_CHARSET     
'ISO-8859-1,utf-8;q=0.7,*;q=0.7'
HTTP_ACCEPT_ENCODING    
'gzip,deflate'
HTTP_ACCEPT_LANGUAGE    
'en-us,en;q=0.5'
HTTP_CONNECTION     
'keep-alive'
HTTP_CONTENT_LENGTH     
'8249'
HTTP_CONTENT_TYPE   
'multipart/form-data; boundary=---------------------------26681719213985'
HTTP_HOST   
'example.com'
HTTP_KEEP_ALIVE     
'115'
HTTP_REFERER    
'http://example.com/user/register/'
HTTP_USER_AGENT     
'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6'
PATH_INFO   
u'/user/register/'
QUERY_STRING    
''
REMOTE_PORT     
'52869'
REQUEST_METHOD  
'POST'
REQUEST_URI     
'/user/register/'
SCRIPT_NAME     
u''
SERVER_PROTOCOL     
'HTTP/1.1'
SERVER_SOFTWARE     
'Apache'
_   
'_'
wsgi.errors     
<open file '<stderr>', mode 'w' at 0x7f2a6026f140>
wsgi.input  
<socket._fileobject object at 0x7f2a5cbc4848>
wsgi.multiprocess   
True
wsgi.multithread    
False
wsgi.run_once   
True
wsgi.url_scheme     
'http'
wsgi.version    
(1, 0)

最后我的追溯:

Traceback:
File "/home/rails/fandrive/site-packages/django/core/handlers/base.py" in get_response
  92.                 response = callback(request, *callback_args, **callback_kwargs)
File "/home/rails/fandrive/registration/views.py" in register
  47.             new_user = backend.register(request, **form.cleaned_data)
File "/home/rails/fandrive/registration/backends/default/__init__.py" in register
  23.                                      request=request)
File "/home/rails/fandrive/site-packages/django/dispatch/dispatcher.py" in send
  166.             response = receiver(signal=self, sender=sender, **named)
File "/home/rails/fandrive/regbackend.py" in user_created
  39.     data.save() 
File "/home/rails/fandrive/site-packages/django/db/models/base.py" in save
  410.         self.save_base(force_insert=force_insert, force_update=force_update)
File "/home/rails/fandrive/site-packages/django/db/models/base.py" in save_base
  483.                     values = [(f, f.get_db_prep_save(raw and getattr(self, f.attname) or f.pre_save(self, True))) for f in meta.local_fields if not isinstance(f, AutoField)]
File "/home/rails/fandrive/site-packages/django/db/models/fields/files.py" in pre_save
  252.             file.save(file.name, file, save=False)
File "/home/rails/fandrive/site-packages/django/db/models/fields/files.py" in save
  91.         self.name = self.storage.save(name, content)
File "/home/rails/fandrive/site-packages/django/core/files/storage.py" in save
  47.         name = self._save(name, content)
File "/home/rails/fandrive/site-packages/django/core/files/storage.py" in _save
  146.             os.makedirs(directory)
File "/bin/python-2.6.1/lib/python2.6/os.py" in makedirs
  150.             makedirs(head, mode)
File "/bin/python-2.6.1/lib/python2.6/os.py" in makedirs
  150.             makedirs(head, mode)
File "/bin/python-2.6.1/lib/python2.6/os.py" in makedirs
  150.             makedirs(head, mode)
File "/bin/python-2.6.1/lib/python2.6/os.py" in makedirs
  150.             makedirs(head, mode)
File "/bin/python-2.6.1/lib/python2.6/os.py" in makedirs
  150.             makedirs(head, mode)
File "/bin/python-2.6.1/lib/python2.6/os.py" in makedirs
  157.     mkdir(name, mode)

任何想法我还应该检查什么?

更新:我的模特

class UserProfile(InheritedProfile):
    def upload_path(self, field_attname):
        filename = hashlib.md5(field_attname).hexdigest()[:4] + "_" + field_attname
        return "uploads/users/%s" % (filename,)

    user = models.ForeignKey(User, unique=True, related_name='profile')
    image = models.ImageField(upload_to=upload_path, verbose_name="Image", blank=True, null=True)

我已经创建了'uploads / users'文件夹,为什么要尝试创建文件夹 - 而不仅仅是文件?

1 个答案:

答案 0 :(得分:2)

运行django应用程序的apache使用没有在媒体目录中创建文件夹/文件的权限。

快速临时修复将是

转到您的媒体文件夹:

/home/fandrive/www/fandrive/site_media

并输入:

sudo chmod -R a+w

使您的文件夹可供所有用户使用。

这种方法可能不安全。为了使其安全,您可以将文件夹的所有权更改为该用户,或创建一个组并将权限分配给该组。