django报错: SuspiciousOperation exception on ImageField upload的原因
作者:我就是个世界
发表于:2011-10-06
今天在用django写了个图片上传测试APP的时候,上传图片就报错如下:
[code]
SuspiciousOperation at /admin/photos/photo/1/
Attempted access to '\upload\201110\2011-09-151302830075_m.jpg' denied.
Request Method: POST
Request URL: http://127.0.0.1:8000/admin/photos/photo/1/
Django Version: 1.3
Exception Type: SuspiciousOperation
Exception Value:
Attempted access to '\upload\201110\2011-09-151302830075_m.jpg' denied.
Exception Location: c:\python27\lib\site-packages\django\core\files\storage.py in path, line 234
Python Executable: E:\workspace\Test\Scripts\python.exe
Python Version: 2.7.2
[/code]
找了半天,没发现代码上有问题,然后搜索,也没同样的问题。
最后还是在google groups中找到了。原因是ImageField字段的属性upload_to指定的这个目录前面不能有斜扛。
[code]
upload_to="upload/%Y%m/" #正确的写法
upload_to="/upload/%Y%m/" #这样就会报错
[/code]
参考:[url=http://groups.google.com/group/django-users/msg/ebf7f00915c5ac8a?dmode=source]http://groups.google.com/group/django-users/msg/ebf7f00915c5ac8a?dmode=source[/url]
[code]
SuspiciousOperation at /admin/photos/photo/1/
Attempted access to '\upload\201110\2011-09-151302830075_m.jpg' denied.
Request Method: POST
Request URL: http://127.0.0.1:8000/admin/photos/photo/1/
Django Version: 1.3
Exception Type: SuspiciousOperation
Exception Value:
Attempted access to '\upload\201110\2011-09-151302830075_m.jpg' denied.
Exception Location: c:\python27\lib\site-packages\django\core\files\storage.py in path, line 234
Python Executable: E:\workspace\Test\Scripts\python.exe
Python Version: 2.7.2
[/code]
找了半天,没发现代码上有问题,然后搜索,也没同样的问题。
最后还是在google groups中找到了。原因是ImageField字段的属性upload_to指定的这个目录前面不能有斜扛。
[code]
upload_to="upload/%Y%m/" #正确的写法
upload_to="/upload/%Y%m/" #这样就会报错
[/code]
参考:[url=http://groups.google.com/group/django-users/msg/ebf7f00915c5ac8a?dmode=source]http://groups.google.com/group/django-users/msg/ebf7f00915c5ac8a?dmode=source[/url]
请发表您的评论