alekosot
alekosot

Reputation: 701

Slugify within a static tag in django templates

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.

Upvotes: 0

Views: 679

Answers (1)

vvd
vvd

Reputation: 476

Use get_static_prefix tag:

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

Upvotes: 1

Related Questions