我在让django玩图片上传方面遇到了一些麻烦。我的脚本将根据日期创建目录,如下所示:
file = models.FileField(upload_to='uploads/%m-%Y/')
现在,如果我在uploads文件夹中创建了日期目录并将文件夹chmod到755,则上传工作正常,但是如果我尝试chmod uploads文件夹而不创建过时的子文件夹(我需要django来做) ,我收到权限错误。
如何创建文件夹以允许创建子文件夹?
这是一个追溯:
Django Version: 1.3
Python Version: 2.5.2
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.admin',
'tagging',
'mediamanager',
'livesettings',
'projects']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware')
Traceback:
File "/var/lib/python-support/python2.5/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/var/lib/python-support/python2.5/django/utils/decorators.py" in _wrapped_view
93. response = view_func(request, *args, **kwargs)
File "/var/lib/python-support/python2.5/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/var/www/enigma-dev/enigma/mediamanager/views.py" in upload_media
24. m.upload_media(data=form.cleaned_data, params=params)
File "/var/www/enigma-dev/enigma/mediamanager/models.py" in upload_media
63. self.save()
File "/var/lib/python-support/python2.5/django/db/models/base.py" in save
460. self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/var/lib/python-support/python2.5/django/db/models/base.py" in save_base
543. for f in meta.local_fields if not isinstance(f, AutoField)]
File "/var/lib/python-support/python2.5/django/db/models/fields/files.py" in pre_save
255. file.save(file.name, file, save=False)
File "/var/lib/python-support/python2.5/django/db/models/fields/files.py" in save
92. self.name = self.storage.save(name, content)
File "/var/lib/python-support/python2.5/django/core/files/storage.py" in save
49. name = self._save(name, content)
File "/var/lib/python-support/python2.5/django/core/files/storage.py" in _save
166. os.makedirs(directory)
File "/usr/lib/python2.5/os.py" in makedirs
171. mkdir(name, mode)
Exception Type: OSError at /media-manager/upload/
Exception Value: [Errno 13] Permission denied: '/var/www/site-dev/site/static/uploads/04-2011'
答案 0 :(得分:20)
运行Python解释器的进程无权写入媒体目录。您需要将chgrp
或chown
媒体目录与Python进程放在同一个组中,并确保目录上至少有g+rwx
,文件上有g+rw
答案 1 :(得分:9)
我收到同样的错误并通过更改修复它:
MEDIA_ROOT = '/media/'
为:
MEDIA_ROOT = 'media/'
在settings.py
下。
答案 2 :(得分:1)
确保您已完成以下操作
你的 settings.py
...
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
...
同时设置媒体目录权限
chgrp -R www-data /path/to/media/
chmod -R g+w /path/to/media/
答案 3 :(得分:0)
对我来说,我忘了添加:MEDIA_ROOT = os.path.join(BASE_DIR,'media')到生产服务器上的settings.py文件中。
答案 4 :(得分:0)
感谢 https://stackoverflow.com/users/10949995/clement-tong 的启发。我在生产环境中遇到了同样的错误,我不得不更改 settings.py 文件以包含前面的正斜杠:
MEDIA_ROOT = os.path.join(BASE_DIR, "/media")
答案 5 :(得分:0)
我遇到了同样的错误,并通过在项目的 setting.py 文件中执行以下操作来修复它:
MEDIA_ROOT = '/media/'
到
MEDIA_ROOT = 'media/'