javascript [removed] image error

2019-09-19 06:16发布

问题:

I am using Flask and pass variables (image_name) to javascript to get dynamic web content. I use those variables within document.write() to display a image file. Depending on the variable image_name the required .jpg file should be loaded.

This is my JS code:

document.write('<img class="img-circle profile_img" '
+ 'src="{{ url_for(' + "'static', filename='images/"
+ "'{{ image_name }}'" + ".jpg' )}}"
+ '" onerror="'
+ "this.onerror=null;this.src='../static/images/"
+ "'{{ image_name }}'"
+ ".jpg';"+'"'+"/>");

With image_name="max_mustermann" I want the output to be:

<img class="img-circle profile_img" src="{{ url_for('static', filename='images/max_mustermann.jpg' )}}" onerror="this.onerror=null;this.src='../static/images/max_mustermann.jpg';"/>

When I run Flask and try to load the page, my broswer is showing the following error.

jinja2.exceptions.TemplateSyntaxError: expected token ',', got 'static'