django - 如何使用模板中存储到 ImageField 的图像?
问题描述
我有以下models.py:
class Insurance(models.Model):
area = models.CharField(max_length=100)
title = models.CharField(max_length=50)
insurance_type = models.CharField(max_length=50)
area_photo = models.ImageField(upload_to='static/image')
这个模板:
{% for product in insurance.insuranceproducts_set.all %}
<li class="one_third btmspace-10 holder">
<figure><img src="{% static 'img/320x320.png' %}" alt="">
<div class="content">
<p><a href="{% url 'insurance:farmer_type' product.id %}">{{ product.title }}</a></p>
</div>
</figure>
</li>
{% endfor %}
问题是页面需要加载关于产品的简短描述。在这里它需要加载与该保险产品相关的图像。我怎样才能做到这一点?
解决方案
文档中有一个很好的总结:https ://docs.djangoproject.com/en/2.1/faq/usage/#how-do-i-use-image-and-file-fields
尝试将您的图像标签更改为::
<img src="{{insurance.area_photo.url}}" alt="">
推荐阅读
- python - 我的变量未缩进时显示为红色
- graphics - 如何根据它在three.js中的局部轴在3D空间中绘制一条垂直于另一条线(3D对象)的线?
- apple-push-notifications - 我的 Apple 推送通知因 Java Apns 停止工作而停止
- python - 如何修复 PyCharm 中的 Python 多处理映射内存错误
- apache - RewriteRule 表现奇怪,将查询添加到某些 url 而不是其他一些
- python - Django POST 返回空 - DRF + recaptcha
- html - 我的边距顶部没有应用于我的锚标记
- excel - Visual Basic 得到 424 对象必需错误
- wordpress - WordPress 用户角色限制插件
- javascript - 如何使用 select2 库多选重复值