Django Static Image Not Displaying When Using Variable for Image Path

I'm trying to load and display a static image in a Django template. I have a dictionary named post that contains an image value, for example, "mountain.jpg". I checked, and the dictionary has the correct data.

However, when I try to set the image path dynamically using post.image, the image is not displayed. If I hardcode the URL, the image shows up correctly.

Here is my image tag in the template

   // not working
   <img src="{% static "blog/images/"|add:post.image %}" alt="Mountain" />

   // working 
   <img src="{% static "blog/images/mountain.jpg" %}" alt="Mountain" />   

the correct syntax to concatenate the static path with the dynamic image name. Change your template code to:

<img src="{% static 'blog/images/' %}{{ post.image }}" alt="Mountain" />

Or if post.image is a Django ImageField or FileField you should use:

<img src="{{ post.image.url }}" alt="Mountain" />
Back to Top