Django - 媒体上传 [Errno 13] 权限被拒绝

发布于 2024-10-30 14:56:41 字数 2645 浏览 7 评论 0原文

我在让 django 很好地处理图像上传时遇到了一些麻烦。我的脚本将根据日期创建目录,如下所示:

file = models.FileField(upload_to='uploads/%m-%Y/')

现在,如果我在上传文件夹中创建带日期的目录并将该文件夹 chmod 为 755,则上传工作正常,但如果我尝试 chmod 上传文件夹而不创建带日期的子目录文件夹(我需要 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'

I'm having some trouble getting django to play nice with image uploads. My script will create directories based on the date like so:

file = models.FileField(upload_to='uploads/%m-%Y/')

Now, if I create the dated directory in the uploads folder and chmod the folder to 755, the upload works fine, but if I try to chmod the uploads folder without creating the dated sub folder (which I need django to do), I am getting a permissions error.

How do I make it so a folder will allow the creation of sub folders?

Here is a traceback:

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'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

揽月 2024-11-06 14:56:41

我遇到了同样的错误,并通过将以下内容更改

MEDIA_ROOT = '/media/'

为:

MEDIA_ROOT = 'media/'

Under settings.py 来修复它。

I was getting the same error and fix it by changing:

MEDIA_ROOT = '/media/'

to:

MEDIA_ROOT = 'media/'

Under settings.py.

岁月染过的梦 2024-11-06 14:56:41

运行 Python 解释器的进程无权写入媒体目录。您需要 chgrpchown 将媒体目录添加到与 Python 进程相同的组,并确保至少有 g+rwx > 对于目录,g+rw 对于文件。

The process that is running your Python interpreter doesn't have permission to write into the media directory. You'll need to either chgrp or chown the media directory to the same group as your Python process, and ensure you have at least g+rwx on directories and g+rw on files.

听,心雨的声音 2024-11-06 14:56:41

确保您已完成以下操作

您的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/

Make sure you have done following

Your settings.py

...
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
...

Also set permission to media directory

chgrp -R www-data /path/to/media/
chmod -R g+w  /path/to/media/
苍景流年 2024-11-06 14:56:41

对我来说,我忘记添加: MEDIA_ROOT = os.path.join(BASE_DIR,'media') 到我的生产服务器上的 settings.py 文件中。

For me, I forgot to add: MEDIA_ROOT = os.path.join(BASE_DIR,'media') to my settings.py file on my production server.

倚栏听风 2024-11-06 14:56:41

感谢 https://stackoverflow.com/users/10949995/clement-tong 的灵感。我在生产环境中遇到了同样的错误,我必须更改 settings.py 文件以包含前面的正斜杠:

MEDIA_ROOT = os.path.join(BASE_DIR, "/media")

Thanks to https://stackoverflow.com/users/10949995/clement-tong for the inspiration. I experienced the same error in a production environment and I had to change settings.py file to include a preceding forward slash:

MEDIA_ROOT = os.path.join(BASE_DIR, "/media")

摘星┃星的人 2024-11-06 14:56:41

我遇到了同样的错误,并通过在项目的setting.py 文件中执行以下操作来修复它:

MEDIA_ROOT = '/media/'

to

MEDIA_ROOT = 'media/'

I was getting the same error and fix it by doing the following in setting.py file of the project:

MEDIA_ROOT = '/media/'

to

MEDIA_ROOT = 'media/'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文