0

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'

4
  • Can you elaborate what exactly you want to achieve? Whatever you are doing, this is not the right approach. Commented Mar 2, 2017 at 11:22
  • There is at least a problem with quotes: You are putting the image_name in single quotes. Commented Mar 2, 2017 at 11:32
  • Possible duplicate of Jinja variables within the Flask url_for function Commented Mar 2, 2017 at 11:35
  • Thanks! The problem was that I used {{}} within {{}}. Commented Mar 2, 2017 at 12:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.