2

I need to concatenate two strings inside a static tag, i checked previous question about how to concatenate strings in django template and i found this answer, but when i used that solution, the broser (Firefox) doesn't show the image.

Details: This is my code, assume that the value of user.if is 1:

{% with "images/my_twitter_wordcloud_"|add:user.id|add:".png" as image_static %} <center> <img src="{% static 'image_static' %}" width="650" height="350" style="margin-left: 10%;"/> </center> {% endwith %}

When i ckecked the inspector in the browser, it showed the image like this:

<img src="/static/image_static" style="margin-left: 10%;" height="350" width="650">

I think that it should be:

    <img src="/static/images/my_twitter_wordcloud_"|add:user.id|add:"png" style="margin-left: 10%;" height="350" width="650">

I checked the django documentation about add reference, and i tried using "" and "" after add, but It seems like django doesn't interpretate the variable, what i'm doing wrong?

Thanks for your help. PD: i'm apologyze for my English.

Edit: Doing what @Selcuk said, the inspector show me this:

<img src="/static/.png" style="margin-left: 10%;" height="350" width="650">

Is strange, even if i use '' instead of "", so i don't understand what is wrong, if i put this (where the value of user.id is 1):

<center><img src="{% static 'images/my_twitter_wordcloud_1.png' %}"

It show the image correctly.

Community
  • 1
  • 1
Manuel Carrero
  • 599
  • 1
  • 9
  • 29

2 Answers2

0

Remove the quotes around image_static and use the variable name. That is, replace the following:

{% static 'image_static' %}

with this

{% static image_static %}

As a side note, you should also include a dot (.) before the image extension (png).

Selcuk
  • 57,004
  • 12
  • 102
  • 110
0

for future people who will come here.

avarage_rating can hold any single value from this list [0,1,2,3,4,5] including decimals.

so use a single variable as a value and the loading dynamic image is below.

{{avarage_rating | first }} == first digit and .svg is appended to it. here s is the name of the image initial.

<code><img src="{% static 'img/s'%}{{avarage_rating | first }}.svg"><span>{{avarage_rating}}</span></code>
optimists
  • 191
  • 1
  • 7