使用Django的ImageField时出现问题

时间:2014-01-26 01:32:32

标签: python django

我定义了以下模型:

class Pizza(models.Model):
    name = models.CharField(max_length=50, blank=False, null= False, unique = True)
    description = models.CharField(max_length=100, blank=False, null= False, unique = True)    
    price = models.ForeignKey('Type')    
    image = models.ImageField(upload_to='images', blank=True, null=True)

我在settings / py上使用以下参数:

# Absolute filesystem path to the directory that will hold user-uploaded files.
MEDIA_ROOT = '/my/files/root/'

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://example.com/media/", "http://media.example.com/"
MEDIA_URL = '/media/'

该文件已正确上传到:

/my/files/root/images

但是,我这样引用它:

<img src="{{ MEDIA_URL }}{{ pizza.image }}"></img>

......链接坏了。

链接显示:

/media/images/imagename.jpg

我能错过什么?

1 个答案:

答案 0 :(得分:0)

试试这个:

{{ pizza.image.url }}