0

I have a line of code <img src="{% static 'mysite/plots/' %}{{ plot }}" alt="Stock price vs. predictions graph" style="width: 600px; height: 450px;"> where I want to display images based on a variable called plot that I pass from my views.py. For example, this variable could be 'AAPL.PNG' or 'TSLA.PNG', whatever the image name is, I am sure that I have it stored at my static/mysite/plots/ directory because when I access these paths directly with {% static 'mysite/plots/AAPL.PNG' %} it works.

I have also tried:

src="{% static 'mysite/plots/{{ plot }}' %}"
src="{% static 'mysite/plots/'(plot) %}"
src="{% static 'mysite/plots/' + plot %}"
Mason Choi
  • 101
  • 2
  • 12
  • Does this answer your question? [How to concatenate strings in django templates?](https://stackoverflow.com/questions/4386168/how-to-concatenate-strings-in-django-templates) – Abdul Aziz Barkat Jul 09 '21 at 13:32
  • I have tried their solutions, but none of them worked. The main one with the 'with' tag and 'include' tag threw a template not found error. – Mason Choi Jul 09 '21 at 13:54
  • 1
    That `include` was for _that_ question (i.e. the OP there wanted to use the include template tag)... Instead of `include` you want to use the `static` template tag... – Abdul Aziz Barkat Jul 09 '21 at 13:57
  • OH! That makes much more sense, thank you! using the with tag and then referencing that tag with {% static plot_url %} was the way to go. Don't forget that {% endwith %} goes after the part where you are referencing the with! – Mason Choi Jul 09 '21 at 14:12

0 Answers0