在img src中使用Django模板标签 [英] Use Django Template Tags in img src

查看:241
本文介绍了在img src中使用Django模板标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Django模板标签显示与每个选择相关的特定图像,但是它似乎不起作用,只是返回损坏的图像。这是我相关的html:

I am trying to use the Django template tags to bring up specific images that correlate with each choice, however it does not seem to work, just return a broken image. Here is my relevant html:

<img class="img-responsive" src="{% static "images/risk_graphics/{{ input.choice }}.jpg" %}"/>

感谢您的帮助。

推荐答案

只需在包含所涉及文件路径的上下文中设置一个变量。您可以在 static 模板标记(在这里称为 special_image_path )中引用变量:

Just set a variable on the context containing the path to the file in question. You can reference variables in the static template tag (here named special_image_path):

<img class="img-responsive" src="{% static special_image_path %}"/>

在此处查看第二个示例在Django文档中

See the second example here in the Django docs

这篇关于在img src中使用Django模板标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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