如何将字符串连接到Django中模板标记内的数字

时间:2013-08-29 19:04:03

标签: django django-templates

我找到了similar question on StackOverflow,但解决方案对我来说似乎不起作用,除非我做错了。我有一个ID号,我想将其附加到模板标签中的字符串中。这是我的尝试:

{% with "image-"|add:vid.the_id as image_id %}
     {# custom template tag to generate image #}
    {% image vid.teaser_thumbnail alt=vid.title id=image_id %}
{% endwith %}

image_id显示为空。

我在这里做错了什么?

我想要的image_id输出就像“image-8989723123”。

1 个答案:

答案 0 :(得分:18)

尝试这种方式(在stringformat之上添加with语句):

{% with vid.the_id|stringformat:"s" as vid_id %}
    {% with "image-"|add:vid_id as image_id %}
         {# custom template tag to generate image #}
         {% image vid.teaser_thumbnail alt=vid.title id=image_id %}
    {% endwith %}
{% endwith %}