2

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?

2

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.