html中imagefield做成链接,python – 如何在模板中获取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

)

我已经将MEDIA_ROOT设置为我的Apache Web根目录下的/ hmedia /目录,并将MEDIA_URL设置为“http:// localhost / hmedia /”.这似乎有效 – 我已经通过Django管理员网站成功上传了几张图片,我可以通过http:// localhost / hmedia / images / [filename]查看这些图像. Django管理员网站实际上显示了我的文件名,并链接到每个图像的实时URL,链接工作.

我的问题是,我无法解决如何在我的模板中获取这些图像的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}}输出任何内容.

如何在我的模板中获取图片的网址? Django管理界面模板怎么做? (我已经扎根在admin模板目录,但找不到我要找的东西.)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值