Home > front end >  How can I create a dynamic src url in html img tag?
How can I create a dynamic src url in html img tag?

Time:02-04

<img src="https://openweathermap.org/img/wn/[email protected]">

The src url changes dynamically by changing the last part for example, by changing [email protected] to [email protected] 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

CodePudding user response:

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

  •  Tags:  
  • Related