[英]How to get substring of django variable [HTML]
I have an img element with an src attribute I am trying to format. 我有一个要设置格式的src属性的img元素。 An example url for would be https://myurl/45/457698373.png , where the 45 is the first 2 characters of the full item.image attribute.
网址示例为https://myurl/45/457698373.png ,其中45是full item.image属性的前2个字符。 My issue is with the {{item.image[0,2]}} part.
我的问题是{{item.image [0,2]}}部分。 How do I get the first 2 characters of this item.image ??
如何获得此项的前2个字符。
<img src='https://myurl/{{item.image[0,2]}}/{{item.image}}.png'>
Django templates do not allow arbitrary Python in them (and Python slices use colons anyway, not commas); Django模板不允许在其中使用任意Python(Python切片无论如何都使用冒号,而不是逗号); what you need here to use Django's template operators.
使用Django的模板运算符所需的条件。 In this particular example, what you want is
在此特定示例中,您想要的是
<img src='https://myurl/{{item.image|slice:":2"}}/{{item.image}}.png'>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.