1

I am trying to get an image to appear using Django template.

In settings I have:

MEDIA_ROOT = 'C:/workspace/mysite/uploaded_media/'
MEDIA_URL = 'http://localhost:8000/mysite/media/'

The html is

<img source = "http://localhost:8000/mysite/media/blabla.jpg"></img>

I have an image file blabla.jpg in the folder uploaded_media

What am I missing here?

Edit: first problem was writing "source" instead of "src" in the tag. doh. (attempting the answer below right now.)

kapex
  • 28,903
  • 6
  • 107
  • 121
user984003
  • 28,050
  • 64
  • 189
  • 285
  • 2
    what do you have in your settings.py / urls.py? http://stackoverflow.com/questions/2237418/serving-static-media-during-django-development-why-not-media-root – Jeremy D Oct 05 '12 at 07:49
  • Not sure I understand. I settings.py I have the above stated settings as well as lots of other stuff. In urls.py, I don't have anything specific for the media. The page is rendered, it just doesn't show the image. – user984003 Oct 05 '12 at 07:54
  • So look at the urls.py shown in the link I mentionned :) – Jeremy D Oct 05 '12 at 07:55
  • Jeremy, oops, I didn't see the link ;) You are right, though, I was missing the pattern in urls.py – user984003 Oct 05 '12 at 08:18

1 Answers1

2

try

MEDIA_URL = '/media/'

and in template

<img src="/media/blabla.jpg" />

Also put these lines of code inside your urls.py and set DEBUG to True

if settings.DEBUG:
  # static files (images, css, javascript, etc.)
  urlpatterns += patterns('',
    (r'^media/(?P<path>.*)$', 'django.views.static.serve', {
    'document_root': settings.MEDIA_ROOT}))
abidibo
  • 4,175
  • 2
  • 25
  • 33
  • look at my edit, the important part, the lines to add to your urls file – abidibo Oct 05 '12 at 07:59
  • Yeah, that did the trick! (Also had to write "from django.conf import settings") But then does this only work in debug mode? Could I just add this pattern to my urlpatterns always, not just if settings.DEBUG? – user984003 Oct 05 '12 at 08:14
  • When you're in production with a web server different from the django one, you'll serve this files statically through the web server by writing you desired roles. You may accept the answer if was useful to you. – abidibo Oct 05 '12 at 08:17
  • Thanks. I'll worry about that when I get to production... It's working great for now. – user984003 Oct 05 '12 at 08:19