我的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管理界面模板是如何做到的?(我已经扎根在管理模板目录中,但找不到我想要的东西。)