Images are not displaying

25 views
Skip to first unread message

Santosh Yelamarthi

unread,
Jun 16, 2017, 7:06:17 AM6/16/17
to Django users
Hi,

am using django 1.10 version. am unable to display images in web.

my app name -- polls

I created templates directory and i put my home.html over there 
<html>
<body>
<img src="static/images/logo.jpg" alt="My Logo"/>
</div>
</body>
</html>

I created static folder and i put my image logo.jpg over there

In my views file I imported home.html :

from django.shortcuts import render_to_response
from django.http import HttpResponse

# Create your views here.
def index(request):
    return render_to_response('home.html')

settings.py :

STATIC_URL = '/static/'

I am able to display text but not images.


Can any one please help me in this.

Thanks in advance

Abraham Varricatt

unread,
Jun 16, 2017, 7:43:09 AM6/16/17
to Django users
If this is a production deployment, you probably need to run collectstatic and test if static files are being served correctly - more a deployment issue than a django matter. 

But, if you are testing with 'runserver' on a development system ... without exact knowledge of your folder structure, it's going to be difficult to help you - since this appears to be a pathing issue. 

However,


On Friday, June 16, 2017 at 7:06:17 AM UTC-4, Santosh Yelamarthi wrote:

I created templates directory and i put my home.html over there 
<html>
<body>
<img src="static/images/logo.jpg" alt="My Logo"/>
</div>
</body>
</html>

I created static folder and i put my image logo.jpg over there

If this is all you did, then shouldn't the src path be "static/logo.jpg" ?

Yours,
Abraham V.

Carl

unread,
Jun 16, 2017, 7:59:43 AM6/16/17
to django...@googlegroups.com
Hi!

Because static is a folder on the root of your website, try putting
a slash in front of static; for example,

<img src="/static/images/logo.jpg" alt="My Logo" />

Hope this helps. :-)

Sincerely,
Carl




--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Antonis Christofides

unread,
Jun 16, 2017, 7:59:53 AM6/16/17
to django...@googlegroups.com

Hello,


<img src="static/images/logo.jpg" alt="My Logo"/>

This is probably missing a slash before "static"; it should be like this:

<img src="/static/images/logo.jpg" alt="My Logo"/>

But a better way is this:

{% load static %}

...

<img src="{% static 'images/logo.jpg' %}" alt="My Logo"/>

If you are on production rather than development, see also this: https://djangodeployment.com/2016/11/21/how-django-static-files-work-in-production/

Regards,

Antonis

Antonis Christofides
http://djangodeployment.com
Reply all
Reply to author
Forward
0 new messages