Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

203
Views
Add an image tag to Jinja Template k from a JavaScript variable

I am trying to create an image tag for a Jinja template with a variable from Ajax.

success: function (response) {
               var pythonResponse = "<ul id='userList'>";
               $.each(response,function(index,value) {
                    console.log(`path is ${value[0]}`)
                    pythonResponse += `<li onClick = 'selectUser("${value[1]}")'> ${value[1]}`
                    pythonResponse += `<img src={{ url_for('static', filename='${value[0])}')}}/></li>`
                    console.log(pythonResponse);
               });

The line in question is this one

pythonResponse += `<img src={{ url_for('static', filename='${value[0])}')}}/></li>`

When I run this code the variable value[0] which contains the file path, is rendered out as a bunch of gobbledygook. This is what it looks like %24%7Bvalue%5B0%5D%29%7D/ but it should look like this /myfolder/myFile.jpg. I have confirmed that the variable does indeed equal what it is suposed to equal and if i hard code the file path into the filename='' then it works. Why is this variable turned from this /myfolder/myFile.jpg into this %24%7Bvalue%5B0%5D%29%7D/?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The order of application here is somewhat confusing. If I'm understanding correctly that the javascript is being applied first, then the answer below should help.

Jinja is escaping your variable for safety. It's possible to introduce security problems in your applications by using a variable the way you are using it here. For information lookup XSS (cross site scripting) attacks for more information.

If you're sure the variable is safe, then Jinja allows you to avoid this encoding with the safe filter.

pythonResponse += `<img src={{ url_for('static', filename='${value[0])}') | safe}}/></li>`

https://jinja.palletsprojects.com/en/2.10.x/templates/#safe

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!