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

Cri*_*owi 0 python django templates image

如果我将我的图像放在/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 %}
Run Code Online (Sandbox Code Playgroud)

Dan*_*kov 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,
        }),
   )
Run Code Online (Sandbox Code Playgroud)