我在“赞助商”模型中包含了一个 ImageField 字段,该字段设置为将上传的图像文件保存到 my_app/static/uploads/sponsor_logos,当我尝试使用管理界面保存记录时,出现以下错误:
/admin/sponsors/sponsor/add/ 处的 OSError [Errno 13] 权限被拒绝:'/static'
这是我的模型:
from django.db import models
class Sponsor(models.Model):
name = models.CharField(max_length=200)
level_choices = (('Gold', 'Gold'), ('Silver', 'Silver'), ('Bronze', 'Bronze'))
level = models.CharField(max_length=10, choices=level_choices)
logo = models.ImageField(upload_to = 'sponsor_logos/')
class Meta:
ordering = ["name"]
def __unicode__(self):
return self.name
我的媒体根设置为:
MEDIA_ROOT = '/static/uploads/'
这是回溯:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/admin/sponsors/sponsor/add/
Django Version: 1.4.2
Python Version: 2.7.2
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.flatpages',
'django.contrib.humanize',
'awards',
'sponsors',
'django.contrib.admin')
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 "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in wrapper
366. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapped_view
91. response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
89. response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/sites.py" in inner
196. return view(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapper
25. return bound_func(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapped_view
91. response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in bound_func
21. return func(self, *args2, **kwargs2)
File "/Library/Python/2.7/site-packages/django/db/transaction.py" in inner
209. return func(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in add_view
955. self.save_model(request, new_object, form, False)
File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in save_model
709. obj.save()
File "/Library/Python/2.7/site-packages/django/db/models/base.py" in save
463. self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/Library/Python/2.7/site-packages/django/db/models/base.py" in save_base
551. result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "/Library/Python/2.7/site-packages/django/db/models/manager.py" in _insert
203. return insert_query(self.model, objs, fields, **kwargs)
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in insert_query
1593. return query.get_compiler(using=using).execute_sql(return_id)
File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
909. for sql, params in self.as_sql():
File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py" in as_sql
872. for obj in self.query.objs
File "/Library/Python/2.7/site-packages/django/db/models/fields/files.py" in pre_save
249. file.save(file.name, file, save=False)
File "/Library/Python/2.7/site-packages/django/db/models/fields/files.py" in save
86. self.name = self.storage.save(name, content)
File "/Library/Python/2.7/site-packages/django/core/files/storage.py" in save
45. name = self._save(name, content)
File "/Library/Python/2.7/site-packages/django/core/files/storage.py" in _save
168. os.makedirs(directory)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py" in makedirs
150. makedirs(head, mode)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py" in makedirs
150. makedirs(head, mode)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py" in makedirs
157. mkdir(name, mode)
Exception Type: OSError at /admin/sponsors/sponsor/add/
Exception Value: [Errno 13] Permission denied: '/static'
我在 my_app/static/uploads/sponsor_logos 目录上运行了 chmod 777,所以它应该是可写的。此外,我运行了 python manage.py shell,并从那里在赞助商徽标目录中创建了一个新的文本文件,因此该目录似乎是可写的。为什么我不断收到此错误?