pythonhtmlflaskjinja2dynamic-url

How can I create a dynamic src url in html img tag?


<img src="https://openweathermap.org/img/wn/10d@2x.png">

The src url changes dynamically by changing the last part for example, by changing 10d@2x.png to 11d@2x.png a different icon can be obtained. However, I want to dynamically change the url in html using Jinja2 and flask

I tried using

img src="https://openweathermap.org/img/wn/{{icon}}@2x.png"

and send the icon variable from server side, but it won't work


Solution

  • When you use render_template in your code, do something like that:

    return render_template('mytemplate.html', icon='11d')
    

    In your template:

    <img src="https://openweathermap.org/img/wn/{{ icon }}@2x.png">
    

    Note the double-curly braces to render the variable. See the documentation