2

It's my first approach to matplotlib.

I have been trying for a while to print my plots in a template. I know how if I do this for just one image, but now I'm trying to modify it and call different images.

I got this and it works perfectly:

 <img src="{% static "/consulta/imagenes/rosavientos.png" %}">

But I'm trying to use this path: "/consulta/imagenes/rosavientos.png" like this:

  <img src="{% static {{ my_path }} %}">

But ID or literal expected after static.

Is it possible to do this in any way?

PS: I also tried this:

In my view:

ruta_estatica = "<img src = \"{% static '/consulta/imagenes/" + nombre_png + ".png' %}\">"

In the template:

  {% autoescape off %}{{ respuesta3 }}{% endautoescape %}
Hugo L.M
  • 1,053
  • 17
  • 31
  • 1
    Try this https://stackoverflow.com/questions/16655851/django-1-5-how-to-use-variables-inside-static-tag – neverwalkaloner Jun 09 '17 at 15:16
  • 1
    Thank you for the link @neverwalkaloner. My Django version is newer (1.11), but that solution still works. I had not seen that question. Thanks – Hugo L.M Jun 09 '17 at 15:29

2 Answers2

3

You can use the with tag to create a variable and use that in the static tag

 {% with '/consulta/imagenes/'|add:nombre_png|add:'.png' as image %}
   <img src="{% static image %}"/>
 {% endwith %}
Brobin
  • 3,241
  • 2
  • 19
  • 35
0

As I had the same problem and the provided answer by @Brobin was a little bit confusing to me I'll try to clarify it a little bit more.

Considering you have the variable:
{{ my_path }}

and you want to use it inside the function:
<img src="{% static 'some path here...' %}">

you can directly input the variable inside the static function without the {{ }} as follows:
<img src="{% static my_path %}">

Now lets assume you want more complex processing like having an 'item' from the 'my_path' as if it was a list of paths and also include some other text to that as a path or file type, you'll have to use the with function:

{% with final_path='MainSite/videos/My_Sample_Video_'|add:my_path.0|add:'.mp4' %}
    <source src="{% static final_path %}" type="video/mp4">
{% endwith %}

Hope that helps.

Mehrdad995
  • 45
  • 1
  • 9