django 1.5 - How to use variables inside static tag
Asked Answered
S

6

136

I'm currently migrating all the static files references in my project to the new {% static %} tag that django 1.5 introduced, but I'm having a problem, in some places I use variables to get the content. With the new tag I can't, is there any way to solve this?

Current code:

<img src="{{ STATIC_URL }}/assets/flags/{{ request.LANGUAGE_CODE }}.gif" alt="{% trans 'Language' %}" title="{% trans 'Language' %}" />

What it should be (this doesn't work):

<img src="{% static 'assets/flags/{{ request.LANGUAGE_CODE }}.gif' %}" alt="{% trans 'Language' %}" title="{% trans 'Language' %}" />
Slippage answered 20/5, 2013 at 18:24 Comment(0)
D
185

You should be able to concatenate strings with the add template filter:

{% with 'assets/flags/'|add:request.LANGUAGE_CODE|add:'.gif' as image_static %}
  {% static image_static %}
{% endwith %}

What you are trying to do doesn't work with the static template tag because it takes either a string or a variable only:

{% static "myapp/css/base.css" %}
{% static variable_with_path %}
{% static "myapp/css/base.css" as admin_base_css %}
{% static variable_with_path as varname %}
Denise answered 20/5, 2013 at 18:45 Comment(1)
What if I want to put a variable{{var}} inside static tag? with tag can only accept one var.Concentric
P
42

For what it's worth, I think this is the easiest way:

<img src="{% static 'assets/flags/'|add:request.LANGUAGE_CODE|add:'.gif' %}" ... >

This is and old question and I'm not sure if this method could be done back then, But now, in Django 2.0 this seems to work fine for me.

Probable answered 28/3, 2018 at 4:45 Comment(5)
I confirm that this method works in Django 2.0, and in my opinion, it is the best way to solve the problem.Acanthocephalan
Works for me, but the docs come with this warning: "Strings that can be coerced to integers will be summed, not concatenated"Propraetor
Does not work with hashed file names (ManifestStaticfilesStorage ).Propraetor
@Propraetor that is actually concerning. but since we know the first argument can't be an integer, in this case it should be safe. Also, does hashed file names have "str" or "add" defined?Probable
Being in Django 3.2.5 it's still the best way IMO.Pl
W
32

a cleaner way is to set the {% static %} as a variable from the beginning of the html so we can use it in any way we want.

{% load static %}
{% static "" as baseUrl %}
<img src="{{ baseUrl }}/img/{{p.id}}"></img>
Womanize answered 3/3, 2015 at 3:37 Comment(2)
This fails if you are using s3 storage with signed URLs, as the Storage backend if not invoked to prepare the URL for each file.Promenade
This looks very nice in a template but this is more of a hack than a clean way of using static.Naughton
T
25

I got this to work by using an empty string for the static path and then using my variables in their own section, like this:

<a href= "{% static "" %}{{obj.a}}/{{obj.b}}/{{obj.c}}.gz" >Name</a>
Toxic answered 13/3, 2014 at 7:47 Comment(3)
this is super clever, but what @horbor said, you can simplify it even more! docs.djangoproject.com/en/1.6/ref/templates/builtins/…Stpeter
Rather than use an empty string, use {% get_static_prefix %}.Physoclistous
This doesn't URL encode any of the {{obj.a}} and so forth.Speer
D
15

@rounin, you can, at least, use

{% get_static_prefix %} 

which will be loaded when you {% load static %}. It's just more natural then {% static '' %} :)

Dhiren answered 18/5, 2014 at 18:21 Comment(1)
This won't work with things like ManifestStaticfilesStorage that changes foo.js into foo.8c9a23d.jsGreatest
D
0

Just put the variables outside of the tag:

src="{% static 'directory/' %}{{filename}}.{{fileextension}}"

Result: "directory/filename.fileextension"

Example Result: "directory/bird.jpg"

Doubler answered 13/2, 2023 at 20:48 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.