0

If I put my image in /media/ folder, it doesn't works. But if I put them in '/static/' folder it works.

{% extends 'base.html' %}

{% block contain %}
    <h1>New Report</h1>
    <form id="create" method="POST" action="" enctype="multipart/form-data">{% csrf_token %}
        {{ form.as_p }}
        <input type="submit" value="Confirm">
    </form>


    {% for report in reports %}
        <P> {{report.title}} </P> <br>
        <img src="{{STATIC_URL}}<name of the image>" width='100px' height='100px'> =====> I can see the image

        <img src="{{MEDIA_URL}} <name of the image>" width='100px' height='100px'> =====> I cant see the image

        <img src="/static/<name of the image>" width='100px' height='100px'> =====> I cant see the image

        <img src="/media/<name of the image>" width='100px' height='100px'> ====>>>> I cant see the image


    {% endfor %}
{% endblock %}
Daniil Ryzhkov
  • 7,416
  • 2
  • 41
  • 58
Cris Towi
  • 1,141
  • 2
  • 10
  • 15
  • How do you deploy your django app? – Daniil Ryzhkov Jul 04 '13 at 04:22
  • Have you read this? https://docs.djangoproject.com/en/1.4/howto/static-files/#serving-static-files-in-development – Daniil Ryzhkov Jul 04 '13 at 04:24
  • But my image is not a static file. My image is an image from my Report model. Whe I put an image in "Media" Folder i cant see the image, but when i use that image en my "Static" folder I can see the image. It's really weird. – Cris Towi Jul 04 '13 at 04:26
  • it's a static file. Your Report model ImageField is just a link to that file. Check out my answer. Django or your webserver probably don't know about your media folder – Daniil Ryzhkov Jul 04 '13 at 04:28
  • If it's an image from your report model just call it directly and django will fill in the link for you. – emschorsch Jul 04 '13 at 04:29
  • I think that my server dont know about my media folder. I will check it. – Cris Towi Jul 04 '13 at 04:31

2 Answers2

6

Are you sure django testing server (or apache/nginx) knows about your media folder?

Try to add this to urls.py (in case you're using development server):

if settings.DEBUG:
    urlpatterns += patterns('',
        url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.MEDIA_ROOT,
        }),
   )
Daniil Ryzhkov
  • 7,416
  • 2
  • 41
  • 58
0

Putting images in your static folder will work, as the files in the static folder are served on the site. This is a potential solution and this discussion seems to suggest it's reasonable for some images. On my site the images in the media folder are located at /uploads/<name of the image>. This is what MEDIA_URL is set to. However I never explicitly call MEDIA_URL as all my images are tied into models so django handles that for me. My guess would be double check the name of the folder you're storing the images in and make sure you're looking at the right place. Second I would make sure that {{MEDIA_URL}} isn't undefined in the template.

Community
  • 1
  • 1
emschorsch
  • 1,619
  • 3
  • 19
  • 33