[Python] How to retrieve an image from static folder using javascript and django

Discussão em 'Python' iniciado por Stack, Julho 2, 2025.

  1. Stack

    Stack Membro Participativo

    I have trouble trying to get an image from static folder. When I pass the image in the html it works. here is my code

    <div id="dirs" class="card mb-3" style="max-width: 540px;">
    <div class="row no-gutters">
    <div class="col-md-4">
    <img src="{% static 'PERDNI/09401576.jpg' %}" class="card-img" alt="..."/>
    </div>
    <div class="col-md-8">
    <div class="card-body">
    <h5 class="card-title">Datos personales</h5>
    <p class="card-text">This is a wider card.</p>
    </div>
    </div>
    </div>
    </div>


    This is the generated html in the browser inspector: [​IMG]

    In other hand, when I generate the html from a javascript file, it doesn't work, The image doesn't appear but the other part of the html yes. Here is my code

    var datahtml='<div class="row no-gutters"><div class="col-md-4">';
    datahtml+='<img src="{% static '
    datahtml+="'PERDNI/09401576.jpg'"
    datahtml+=' %}" class="card-img" alt="My image"/>'
    datahtml+='</div><div class="col-md-8"><div class="card-body">'
    datahtml+='<h5 class="card-title">Card title</h5>'
    datahtml+='<p class="card-text">Hello</p>'
    datahtml+='</div></div></div>'
    document.getElementById("dirs").innerHTML=datahtml


    This is the other generated html in the browser inspector: [​IMG]

    What would be the problem, I was thinking in the quotes however I tried in many ways and It haven't worked yet. Thanks in advance.

    Continue reading...

Compartilhe esta Página