Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django template img src not working

Tags:

image

django

src

I want to print an image by using a img src tag in a Django template file "base.html":

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
    <title>Foto</title>
</head>
<body>
    <h1>My helpful timestamp site</h1>
    <img src="google.png" / >
    <hr>
    <p>Made by ... </p>
</body>
</html>

In views.py I define:

def hello(request):
    return render_to_response('base.html')

But the image does not show up in the browser. If I open it as a simple html file, it shows up in the browser.

like image 761
user1680859 Avatar asked Sep 18 '12 16:09

user1680859


2 Answers

In recent versions of django

<img src="{% static 'path/to/image.ext' %}"/>

like image 102
chandan Avatar answered Oct 11 '22 04:10

chandan


That happens because Django does not know the path to this image.
Make a folder named static/, and then a folder named images/ in your project root(where your settings.py file resides).

my_project/
    my_project/ 
        settings.py
        static/
           images/
             google.png

And then change it to:

<img src="{{STATIC_URL}}images/google.png" / >

More here.

like image 31
thikonom Avatar answered Oct 11 '22 06:10

thikonom