Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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".

like image 446
shrewdbeans Avatar asked Aug 29 '13 19:08

shrewdbeans


1 Answers

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 %}
like image 105
alecxe Avatar answered Oct 20 '22 11:10

alecxe