【发布时间】:2018-01-01 12:11:55
【问题描述】:
图片不能在html中显示。我在models.py中写了代码
class POST(models.Model):
title = models.CharField(max_length=100)
image = models.ImageField(upload_to='images/', blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
在views.py中
def top(request):
content = POST.objects.order_by('-created_at')[:5]
return render(request, 'top.html',{'content':content})
在 top.html 中
<div>
{% for item in content %}
<div>
<h2>{{ item.title }}</h2>
<img src="{{ item.image }}"/>
</div>
{% endfor %}
</div>
当我在浏览器中访问时,会显示 html。显示<h2>{{ item.title }}</h2> 但不存在 <img src="{{ item.image }}"/>。当我在 GoogleChrome 中看到页面源时,显示<img src="/images/photo.jpg"/>。但是当我点击 url 时,会发生 404 错误。我的应用程序有 image/images 文件夹,在 images 文件夹中肯定我上传的图片在那里。我真的不明白为什么会发生这种情况。我在 settings.py 中写了 MEDIA_ROOT&MEDIA_URL 所以我认为图像是在浏览器中显示。我应该如何解决这个问题?
【问题讨论】: