我在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模板目录,但找不到我要找的东西.)