python – Django – 显示ImageField
发布时间:2021-01-17 08:14:38 所属栏目:Python 来源:互联网
导读:我刚刚开始使用 django,我还没有找到很多关于如何显示一个imageField的信息,所以我做到了: models.py class Car(models.Model): name = models.CharField(max_length=255) price = models.DecimalField(max_digits=5, decimal_places=2)
我刚刚开始使用 django,我还没有找到很多关于如何显示一个imageField的信息,所以我做到了: models.py class Car(models.Model): name = models.CharField(max_length=255) price = models.DecimalField(max_digits=5,decimal_places=2) photo = models.ImageField(upload_to='site_media') views.py def image(request): carx = Car() variables = RequestContext(request,{ 'carx':carx }) return render_to_response('image.html',variables) image.html {% extends "base.html" %} {% block content %} <img src=carx /> {% endblock %} 我已经保存了一个图像,因为终端,我知道是在那里,如果在image.html中执行此操作: {% block content %} {{ carx }} {% endblock %} 输出是:车对象 任何人都可以告诉我我的错误在哪里? 非常感谢. 解决方法ImageField包含一个url属性,您可以在模板中使用该属性来呈现正确的HTML.{% block content %} <img src="{{ carx.photo.url }}"> {% endblock %} (编辑:百色站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Python中实现常量(Const)功能
- Python求两个文本文件以行为单位的交集、并集与差集的方法
- python exceptions.UnicodeDecodeError:’ascii’编解码器
- Pythonic计算pandas数据帧条纹的方法
- python – UnicodeDecodeError:’utf8’编解码器无法解码位
- python – 如何计算熊猫中一行中所有元素的加权和?
- python – TensorFlow:Hadamard产品::我如何得到这个?
- python – numpy.array的部分内容
- python – CherryPy日志记录:如何配置和使用全局和应用程序
- Python可以生成类似于bash的set -x的跟踪吗?