0

In the view.py I have a list like this:

context['cities'] = ['London', 'Beijing', 'Cape Town', ..]

I don't know why I can't substitute substring in string in the following Html code:

{% for city in cities % }
  <di style="background-image: url({% static 'app/img/{{city}}.jpg' %});">
{% endfor %}

I expect something like app/img/London.jpg.

in inspect what I see is this string:

url(/static/app/img/%7B%7Bcity%7D%7D.jpg);
ElisaFo
  • 130
  • 13
  • It looks like you are using a Python web server that reads template HTML files and inserts text in them before sending them out. What server is that? Flask? Django? Something else? In other words, what libraries do you import in your main Python file? – Matthias Fripp Sep 19 '20 at 07:39
  • I updated the tags. I am using django, and this is django template, – ElisaFo Sep 19 '20 at 07:41
  • Tagging django should help. You may also find some info here: https://docs.djangoproject.com/en/1.8/ref/templates/builtins/ . It looks like they usually leave blank spaces just inside the curly brackets in those template files. Maybe that would help? – Matthias Fripp Sep 19 '20 at 07:43
  • Thanks for the link, I had a look to this doc before, I also tried `{% cycle static 'app/img/{{city}}.jpg' %}` however didn't work. Not sure if this is the right approach – ElisaFo Sep 19 '20 at 07:46
  • Does `{% cycle static 'app/img/{{ city }}.jpg' %}` or `{% cycle static {{ 'app/img/{}.jpg'.format(city) }} %} ` work any better? I don't use django so I'd have to read the docs to suggest anything more. – Matthias Fripp Sep 19 '20 at 07:50
  • No, unfortunately none of them work. Django template does not support python `.format()` – ElisaFo Sep 19 '20 at 08:47
  • How about ``? – Matthias Fripp Sep 19 '20 at 17:52
  • @MatthiasFripp Thanks, I solved by using this string `"/static/app/img/top-cities/{{ city }}.jpg"` – ElisaFo Sep 20 '20 at 17:25

2 Answers2

0

I could solve problem with just a bit editing my code:

{% for city in cities % }
  <di style="background-image: url("/static/app/img/top-cities/{{ city }}.jpg">
{% endfor %}
ElisaFo
  • 130
  • 13
-1

In python, {{, }} stands for normal curly braces, not for putting a value within them.

So {{city}} doesn't return the value of the city variable, it returns {city}.

The double braces for returning a literal brace is complicated and doesn't make as much sense as \{, but it is what it is.

Robo Mop
  • 3,485
  • 1
  • 10
  • 23
  • Here's the link to the official python docs for this: [stupid double curly braces make no sense](https://docs.python.org/3.3/library/string.html#format-string-syntax) – Robo Mop Sep 18 '20 at 21:59
  • Thanks for your answer. However I didn't understand, you mean it should be written like `app/img/cities/\{{city}}.jpg` in html? – ElisaFo Sep 18 '20 at 22:02
  • my question is about html, python was example. let me update – ElisaFo Sep 18 '20 at 22:03
  • Ahhh I see. Take a look at [this question](https://stackoverflow.com/questions/32717081/html-code-with-curly-braces) then. – Robo Mop Sep 18 '20 at 22:03
  • unfortunately could't find any straight forward solution for my question. – ElisaFo Sep 18 '20 at 22:38