0

In one of my templates I use

{% for key, value in dict.items %}
    ...
    <img src="{% static 'img/{{ key|slugify }}.jpg' %}"/>
    ...
{% endfor %}

but the output is not the one expected so I guess that one cannot use template filters within a static tag.

Any suggestions on how to achieve this then?

Edit: the keys of the dict above are strings that corresponds to the filenames of the jpg images.

alekosot
  • 661
  • 9
  • 19
  • what is the error you're experiencing? why not storing the images as objects with a `slug` field? – Samuele Mattiuzzo Jun 25 '13 at 09:06
  • The error was a sort of "urlencoded" string instead of the filename (even though there were no special or nonlatin characters in the key). As far as the image as objects question, I don't want the extra queries to the db for the specific images. – alekosot Jun 25 '13 at 13:22

1 Answers1

1

Use get_static_prefix tag:

{% load static %}
{% for key, value in dict.items %}
    ...
    <img src="{% get_static_prefix %}img/{{ key|slugify }}.jpg"/>
    ...
{% endfor %}
vvd
  • 476
  • 2
  • 5