pin*_*422 3 django upload imagefield
我已经编写了一个简单的django应用程序来测试ImageField,但我遇到的问题是upload_to似乎不起作用.以下是代码:
  1 from django.db import models
  2 
  3 # Create your models here.
  4 class TestImage(models.Model):
  5     img = models.ImageField(max_length=256, upload_to='images')
在我的settings.py中,我有:
  2 from os.path import dirname, join, abspath
  3 __dir__ = dirname(abspath(__file__))
 50 MEDIA_ROOT = join(__dir__, 'static')
 55 MEDIA_URL = '/media/'
然后我使用manage.py启动python shell:
jchin@ubuntu:~/workspace/testimage$ ./manage.py shell
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import settings
>>> from image_app.models import TestImage
>>> p = TestImage(img='test.jpg')
>>> p.save()
>>> p.img.name
'test.jpg'
>>> p.img.path
u'/home/jchin/workspace/testimage/static/test.jpg'
>>> p.img.url
'/media/test.jpg'
从结果中可以看出,django完全忽略了我的'upload_to'参数.我无法弄清楚为什么.从文档中我应该期望p.img.path返回"/home/jchin/workspace/testimage/static/images/test.jpg"并在DB中存储"images/test.jpg",对吗?但DB只是存储文件名:
mysql> select * from image_app_testimage;
+----+-----------+
| id | img       |
+----+-----------+
|  1 | test.jpg  |
+----+-----------+
1 rows in set (0.00 sec)
我检查了所有的文件,我找不到我做错了什么.有人有主意吗?我正在使用django 1.2.5,它应该支持upload_to.
请帮忙!约翰
| 归档时间: | 
 | 
| 查看次数: | 3137 次 | 
| 最近记录: |