如何在模板中获取ImageField URL? [英] How to get an ImageField URL within a template?

查看:100
本文介绍了如何在模板中获取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 =
editable = False,
default = 0

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



我的问题是,我无法工作了解如何在我的模板中获取这些图像的URL或文件名:

 < ul> 
{%for article in image.image_set.all%}
< li> Caption:{{image.caption}},Credit:{{image.credit}},URL: {{image.url}} < /锂>
{%endfor%}
< / ul>

上述模板产生了以下输出:

 < UL> 

< li>标题:这里是图片说明,信用:一些摄影师,URL:< / li>

< li>标题:另一个标题在这里,信用:另一位摄影师,URL:< / li>

< / ul>

换句话说,它正确地输出了每个图像的标题和信用属性,但它没有输出{{image.url}}。



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

解决方案

你需要 {{image.image.url}} & {{image.image.path}} ,而 {{image}} - 只是一个Image对象


I've got the following model in my Django app:

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
    )

I've set the MEDIA_ROOT to a directory called /hmedia/ inside my Apache web root, and I've set MEDIA_URL to 'http://localhost/hmedia/'. This seems to have worked – I've successfully uploaded a couple of images through the Django admin site, and I can view those images via http://localhost/hmedia/images/[filename]. And the Django admin site actually shows me the filename, and links to the live URL for each image, and the links work.

My problem is, I can't work out how to get the URLs or filenames of these images in my template:

<ul>
{% for image in article.image_set.all %}
    <li>Caption: "{{image.caption}}", Credit: "{{image.credit}}", URL: "{{image.url}}"</li>
{% endfor %}
</ul>

The above template results in this output:

<ul>

    <li>Caption: "here's an image caption", Credit: "some photographer", URL: ""</li>

    <li>Caption: "Another caption here", Credit: "Another photographer", URL: ""</li>

</ul>

In other words, it correctly outputs the caption and credit properties of each image, but it outputs nothing for {{image.url}}.

How do I get the image's URL in my template? How does the Django admin interface template do it? (I've rooted around in the admin templates directory but can't find what I'm looking for.)

解决方案

You need {{ image.image.url }} & {{ image.image.path }}, while {{ image }} - just an Image object

这篇关于如何在模板中获取ImageField URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆