我找到了一个StackOverflow 上有类似问题 https://stackoverflow.com/questions/4386168/how-to-concatenate-strings-in-django-templates,但该解决方案似乎对我不起作用,除非我做错了。我有一个 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 %}
But image_id
出来的时候是空的。
我在这里做错了什么?
我想要的输出image_id
类似于“image-8989723123”。
试试这个方法(已添加with
声明与字符串格式 https://docs.djangoproject.com/en/dev/ref/templates/builtins/#stringformat在你的上面):
{% 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 %}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)