include image files in Django templates shows broken link?

你离开我真会死。 提交于 2020-01-05 04:59:08

问题


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!


回答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




回答2:


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)



回答3:


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" />



回答4:


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.



来源:https://stackoverflow.com/questions/5016864/include-image-files-in-django-templates-shows-broken-link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!