Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

204
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda