JimJim
JimJim

Reputation: 3

Django/python/static files/Jinja, How to concatenate string and jinja expression INSIDE jinja statement

How to concat a string and a jinja expression inside jinja statement?

{% for pic in pictures %}
    {% if pic.name == line.name %}
        <img class="card-img-top" src="{% static 'orders/img/'pic.picture %}" >
    {% endif %}
{% endfor %}

In this tag with Jinja, the pic.picture is a jinja express but how to concat with 'orders/img/'?

Image: 1

Thanks.

Upvotes: 0

Views: 1193

Answers (1)

Oleg Russkin
Oleg Russkin

Reputation: 4404

Default Django Template Language looks like Jinja but is not one - it has it's own filters and does not support all Jinja tags/filters.

Available options with Django built-in filters:

  • join strings with add filter (although it is not recommended as will try to convert to integer, more about it here, but if part is clearly a string...):
src="{% static 'orders/img/'|add:pic.picture %}"
  • no join, resolve base static path and append file name after:
src="{% static 'orders/img/' %}{{ pic.picture }}"
{% get_static_prefix as STATIC_PREFIX %}
src="{{ STATIC_PREFIX }}orders/img/{{ pic.picture }}"

Jinja2 can be configured as django template backend.

Upvotes: 3

Related Questions