简体   繁体   English

Django将变量传递到字符串中

[英]Django passing variables into string

I'm trying to pass: 我正在尝试通过:

>img src="{% static '/static_dirs/images/itcg/01_{{ card_id }}.jpg' %}" alt="{{ card_id }}">

to load image with file name 01_xx.jpg, but the result is just xx without image. 加载文件名为01_xx.jpg的图像,但结果是没有图像的xx。 Basically what happens is that {{ card_id }} works in alt but I can't load the image. 基本上发生的事情是{{card_id}}可以在alt中工作,但是我无法加载图像。 I have tried to change 01_{{ card_id }}.jpg into 01_01.jpg and it works, so the static files can load properly but apparently 01_{{ card_id }}.jpg is not working as intended. 我试图将01 _ {{card_id}}。jpg更改为01_01.jpg并可以正常工作,因此可以正确加载静态文件,但显然01 _ {{card_id}}。jpg无法正常工作。 I also tried to changing one of my image name from 01_01.jpg into 01.jpg and do the following: 我还尝试将我的图像名称之一从01_01.jpg更改为01.jpg并执行以下操作:

>img src="{% static '/static_dirs/images/itcg/{{card_id}}.jpg' %}" alt="{{ card_id }}">

The image still not loading, so I guess the problem is not in "01_" part either. 该图像仍未加载,因此我猜问题也不在“ 01_”部分中。 Can someone help? 有人可以帮忙吗?

The {{ .. }} embedded within a {% ... %} doesn't look good. 嵌入{% ... %}中的{{ .. }}看起来不太好。 Write like this: 这样写:

img src="{% static '/static_dirs/images/itcg' %}/{{ card_id }}.jpg" alt="{{ card_id }}">

That is, this will get the correct base url for the static image (the directory), and then you simply append the rest of the image file name. 也就是说,这将为静态图像(目录)获得正确的基本URL,然后您只需附加其余图像文件名即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM