Python 未找到:/products/images/DSroadmap.png未找到:/products/images/codewall.jpg[26/Jan/2020 22:13:36]
我无法从html页面上的URL加载图像。下面是我上传图片、媒体url和html的代码。我们将非常感谢您的帮助Python 未找到:/products/images/DSroadmap.png未找到:/products/images/codewall.jpg[26/Jan/2020 22:13:36],python,html,django,Python,Html,Django,我无法从html页面上的URL加载图像。下面是我上传图片、媒体url和html的代码。我们将非常感谢您的帮助 html: <div class="row"> {% for item in items %} <div class='col-sm-3'> {% for item in item.itemimage_set.all %} {% if item.featured %}
html:
<div class="row">
{% for item in items %}
<div class='col-sm-3'>
{% for item in item.itemimage_set.all %}
{% if item.featured %}
<img src="{{ MEDIA_URL }}{{ item.image }}">
{% endif %}
{% endfor %}
{{ item.title }}
{{ item.price }}
</div>
{% cycle "" "" "" "</div><br/><hr/><div class='row'>" %}
{% endfor %}
</div>
model:
class ItemImage(models.Model):
item = models.ForeignKey(Item, on_delete=models.CASCADE)
image = models.ImageField(upload_to='products/images/')
featured = models.BooleanField(default=False)
thumbnail = models.BooleanField(default=False)
active = models.BooleanField(default=True)
updated = models.DateTimeField(auto_now_add=False, auto_now=True)
def __unicode__(self):
return self.item.title
media url & root:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static', 'media')
html:
{items%%中的项的%s}
{item.itemimage_set.all%}
{%if item.featured%}
{%endif%}
{%endfor%}
{{item.title}
{{item.price}}
{%cycle”“”“”
“%}
{%endfor%}
型号:
类ItemImage(models.Model):
item=models.ForeignKey(item,on_delete=models.CASCADE)
image=models.ImageField(上传到class='products/images/')
featured=models.BooleanField(默认值=False)
缩略图=models.BooleanField(默认值=False)
active=models.BooleanField(默认值=True)
updated=models.DateTimeField(auto\u now\u add=False,auto\u now=True)
def ___; unicode(自):
返回self.item.title
媒体url和根目录:
媒体URL='/MEDIA/'
MEDIA\u ROOT=os.path.join(基本目录'static','MEDIA')
要从FileField()
对象访问图像URL,您需要通过.URL
属性访问它
在本例中,它将是item.image.url
,而不是item.image
,它只返回对象(而不是您需要的url)
Django文件:
您是否尝试使用
item.image.url
而不是item.image
?没问题!我添加了它作为答案,以防其他人偶然发现相同的问题。