Django无法在/ media /文件夹中看到图像

时间:2013-07-04 04:08:00

标签: python django image templates

如果我将图片放在/media/文件夹中,则无效。但是如果我将它们放在'/ static /'文件夹中就可以了。

{% 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 %}

2 个答案:

答案 0 :(得分:6)

你确定django测试服务器(或apache / nginx)知道你的媒体文件夹吗?

尝试将此添加到urls.py(如果您使用的是开发服务器):

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

答案 1 :(得分:0)

将图像放入静态文件夹将起作用,因为静态文件夹中的文件是在网站上提供的。这是一个潜在的解决方案,这discussion似乎表明它对某些图像是合理的。在我的网站上,媒体文件夹中的图像位于/uploads/<name of the image>。这是MEDIA_URL设置的内容。但是我从来没有明确地调用MEDIA_URL,因为我的所有图像都被绑定到模型中,所以django会为我处理。我的猜测是仔细检查您存储图像的文件夹的名称,并确保您正在查看正确的位置。其次,我会确保模板中的{{MEDIA_URL}}不是undefined

相关问题