How to concatenate a string to a number inside a template tag in Django

杀马特。学长 韩版系。学妹 提交于 2019-12-20 21:58:10

问题


I've found a similar question on StackOverflow, but the solution doesn't seem to work for me, unless I'm doing it wrong. I have a ID number, which I'd like to append to a string in a template tag. Here's my attempt:

{% 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 is coming out as empty.

What am I doing wrong here?

My desired output of image_id would be something like "image-8989723123".


回答1:


Try this way (added with statement with stringformat on top of yours):

{% 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 %}


来源:https://stackoverflow.com/questions/18519109/how-to-concatenate-a-string-to-a-number-inside-a-template-tag-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!