django对图片上传的可疑操作

时间:2013-09-05 22:50:02

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

在我的管理员中,我尝试上传图片,点击保存后出现此错误:

SuspiciousOperation at /admin/the_chooser/book/add/
Attempted access to '/media/51VqHa8exoL.jpg' denied.
Request Method: POST
Request URL:    http://localhost/admin/the_chooser/book/add/
Django Version: 1.5.2
Exception Type: SuspiciousOperation
Exception Value:    
Attempted access to '/media/51VqHa8exoL.jpg' denied.
Exception Location: /Users/username/.virtualenvs/django_books/lib/python2.7/site-packages/django/core/files/storage.py in path, line 259
Python Executable:  /usr/bin/python
Python Version: 2.7.2
Python Path:    
['/Library/Python/2.7/site-packages/pip-1.3.1-py2.7.egg',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
 '/Library/Python/2.7/site-packages',
 '/Library/Python/2.7/site-packages/setuptools-0.6c11-py2.7.egg-info',
 '/Users/username/Projects/django_books/',
 '/Users/username/.virtualenvs/django_books/lib/python2.7/site-packages']

我刚刚将Python Imaging Library安装到了virtualenv。这是我的models.py文件:

from django.db import models

LITERARY_TYPE = (
    ('F', 'Fiction'),
    ('N', 'Non-ficiton'),
)

class Book(models.Model):
    name        = models.CharField(max_length=200)
    author      = models.CharField(max_length=200,blank=True,null=True)
    slug        = models.SlugField(unique=True)
    literary_type   = models.CharField(max_length=1, choices=LITERARY_TYPE)
    description = models.TextField(blank=True)
    added       = models.DateField(blank=True,null=True,auto_now_add=True)
    cover       = models.ImageField(blank=True,null=True,upload_to='/media/')

    def __unicode__(self):
        return self.name

这是我第一次使用Django中的文件字段。我的upload_to参数是否正确?还有其他问题吗?

1 个答案:

答案 0 :(得分:6)

删除upload_to路径中的第一个斜杠,使其显示为:     upload_to = '媒体/'