1

All,

I've tried the tips found on the forum. Unfortunately i'm still stuck. The image doesn't appear in the template. As far as i've traced the errors now they are twofold:

1) one where the picture should appear there is still a broke link and an error message:

Failed to load resource: the server responded with a status of 404 (NOT FOUND),
**http://127.0.0.1:8000/Point3D/grafiek/laptop.jpg**

So it does go in the right path, but can't find the image.. Which is in my MEDIA_ROOT or I receive:

Resource interpreted as image but transferred with MIME type text/html with:
**http://127.0.0.1:8000/Point3D/grafiek/**

2) The other error is that if i go to : http://127.0.0.1:8000/media/ i get:

Permission denied: /media/

maybe this has something to do with it.

For the rest i've followed roberts solution for now but without any luck..

Any help would be very much appreciated!

Community
  • 1
  • 1
Tijl
  • 13
  • 1
  • 6

4 Answers4

1

What's your template looking like? It's should be something like:

<img src="{{ MEDIA_URL }}Point3D/grafiek/laptop.jpg" alt="" />

I'm guessing the right path for your image should be http://127.0.0.1:8000/media/Point3D/grafiek/laptop.jpg

Bjorn
  • 5,272
  • 1
  • 24
  • 35
  • this is the path the link on the website directs to but it still doesn't find the image... it must be something silly... The file is in C:\Users\Tijl\Documents\Programming\Dashboard\src\DashboardDesign\FigureOnWebSite\templates\images and my MEDIA_ROOT = 'C:/Users/Tijl/Documents/Programming/Dashboard/src/DashboardDesign/FigureOnWebSite/templates/images/static' with MEDIA_URL = 'http://localhost:8000/static/' my HTML is as you suggested. Any tips? – Tijl Feb 16 '11 at 19:23
  • Try setting MEDIA_ROOT = C:/../images (not C:/../images/static). If MEDIA_URL is localhost:8000/static, the file should be in localhost:8000/static/Point3D/grafiek/laptop.jpg. – Bjorn Feb 17 '11 at 15:55
1

Permission denied on /media/ is exactly what is supposed to happen, as otherwise every user of you page could view all the files in that directory (Would be the same as configuring Apache to allow Indexing of a Directory, which is, in allmost al cases, not what you want. Try accessing the file in your browser by pasting the complete url, (and include the media url in your path!):

http://127.0.0.1:8000/media/Point3D/grafiek/laptop.jpg

If that does not work, make sure you have Django or Apache configured to serve the static files you try to access.

edit:
And one more thing that could cause your trouble: /media/ is the default for the admin media url. If your admin media url and your media url are the same thats no good. Change that so your MEDIA_URL and your ADMIN_MEDIA_PREFIX are not the same.

marue
  • 5,588
  • 7
  • 37
  • 65
  • Going directly to the page u suggested still gives an error. I've tried everything to configure django in such a way that i could find the file.. Somehow there must still be something wrong with my static files? Any tips on how to check this? – Tijl Feb 16 '11 at 19:13
  • Difficult to say, if you do not provide the error message. But i have added one more possible solution to my answer. – marue Feb 16 '11 at 20:26
  • Thanks again, i did check this. I've started a new question and posted most of my code in http://stackoverflow.com/questions/5021909/django-image-trouble-broken-link-in-template. The error i get is a broken image picture and if i right click there an error message Failed to load resource: the server responded with a status of 404 (NOT FOUND) Hope this information could help you helping me. Tanks! – Tijl Feb 16 '11 at 20:49
1

Are you including {{ MEDIA_ROOT }} in your tag.

Also, make sure you are calling the template with a RequestContext object, because otherwise {{ MEDIA_ROOT }} will not be set in your template. Try using django.views.generic.simple.direct_to_template rather than django.shortcuts.render_to_response. Note that direct_to_template is called just like render_to_response except it takes request as the first argument.

from django.views.generic.simple import direct_to_template
# your view code here
return direct_to_template(request, 'path/to/template.html', **kwargs)
Luke Sneeringer
  • 9,270
  • 2
  • 35
  • 32
  • Everything I've tried so far still leads to no file.. http://localhost:8000/static/Point3D/grafiek/laptop.jpg error. I don't know if i need a special folder for static? why can't it find the file? It is in the directory i say it should be.. in this case: C:\Users\Tijl\Documents\Programming\Dashboard\src\DashboardDesign\FigureOnWebSite\templates\images – Tijl Feb 16 '11 at 19:19
1

Assuming media on your file system is at:

/home/username/django-project/media/images/favicon.ico

In your settings.py, you should have:

import os

PROJECT_PATH = os.path.dirname(__file__)
MEDIA_ROOT = os.path.join(PROJECT_PATH, 'media')
MEDIA_URL = '/media/'

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.media',
)

Your project urls.py should also contain the following which should only work in DEBUG mode:

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

Your template should include media like the following:

<link rel="icon" href="{{ MEDIA_URL }}images/favicon.ico" />
Thierry Lam
  • 45,304
  • 42
  • 117
  • 144