django对图片上传的可疑操作
问题描述
在我的管理员中,我尝试上传图片,点击保存后出现此错误:
In my admin I try to upload an image and after clicking save I get this error:
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 文件:
I just installed the Python Imaging Library to my virtualenv. This is my models.py file:
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
参数是否正确?还有别的问题吗?
This is my first time working with file fields in Django. Is my upload_to
parameter correct? Is something else the issue?
解决方案
删除你的upload_to路径中的第一个斜杠,使其显示为:upload_to='媒体/'
Remove the first slash in your upload_to path so that it reads: upload_to='media/'
相关文章