Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

205
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda