views:

317

answers:

1

I tried to use an image file in my HTML template. But when I run the server and open it in the browser the image is not getting loaded. But if I run the HTML file individually it's working fine. I mentioned the path of images folder in file settings.py, MEDIA_ROOT, also. Still it is not working.

settings.py:

MEDIA_ROOT = 'C:/Users/Vicky/Documents/Django/krish/media/images/'

HTML:

<img src="../media/images/Untitled.jpg" width ="267" height="122">

I also tried giving:

<img src="Untitled.jpg" width ="267" height="122">

How can it be made to work?

+1  A: 

I had this problem when I started Django too :)

The Django server does not serve static files by default. Usually you need to use a separate server for this, but in a development environment you can use a shortcut.

Add this to your urls.py:

if settings.DEBUG:
    urlpatterns += patterns('',
        (r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/path/to/media'}),
    )

Don't use this in production. It is slow, unstable and insecure.

freiksenet
what is the use of MEDIA_ROOT?
Vicky
For example, Django saves uploaded files there by default.
freiksenet

related questions