html中imagefield做成链接,如何在模板中获取ImageField URL?

我的Django应用中有以下模型:

class Image(models.Model):

image = models.ImageField(

upload_to='images/',

height_field='height',

width_field='width'

)

credit = models.CharField(max_length=50, blank=True)

caption = models.TextField(blank=True)

article = models.ForeignKey(Article)

width = models.PositiveIntegerField(

blank = True, null = True,

editable = False,

default = 0

)

height = models.PositiveIntegerField(

blank = True, null = True,

editable = False,

default = 0

)

我的问题是,我不知道如何在模板中获取这些图像的URL或文件名:

{% for image in article.image_set.all %}

Caption: "{{image.caption}}", Credit: "{{image.credit}}", URL: "{{image.url}}"

{% endfor %}

上面的模板产生以下输出:

  • Caption: "here's an image caption", Credit: "some photographer", URL: ""
  • Caption: "Another caption here", Credit: "Another photographer", URL: ""

换句话说,它可以正确输出每个图像的标题和字幕属性,但是对于{{image.url}}则不输出任何内容。

如何在模板中获取图像的URL?Django管理界面模板是如何做到的?(我已经扎根在管理模板目录中,但找不到我想要的东西。)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值