If I put my image in /media/
folder, it doesn't works. But if I put them in '/static/' folder it works.
如果我将我的图像放在/ 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 个解决方案
#1
5
Are you sure django testing server (or apache/nginx) knows about your media folder?
你确定django测试服务器(或apache / nginx)知道你的媒体文件夹吗?
Try to add this to urls.py (in case you're using development server):
尝试将此添加到urls.py(如果您使用的是开发服务器):
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
)
#2
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.
将图像放在静态文件夹中将起作用,因为静态文件夹中的文件是在站点上提供的。这是一个潜在的解决方案,这个讨论似乎表明它对某些图像是合理的。在我的网站上,媒体文件夹中的图像位于/ uploads / <图像名称> 。这就是MEDIA_URL的目的。但是我从未明确地调用MEDIA_URL,因为我的所有图像都绑定到模型中,所以django会为我处理。我的猜测是仔细检查您存储图像的文件夹的名称,并确保您正在查看正确的位置。其次,我要确保模板中未定义{{MEDIA_URL}}。
#1
5
Are you sure django testing server (or apache/nginx) knows about your media folder?
你确定django测试服务器(或apache / nginx)知道你的媒体文件夹吗?
Try to add this to urls.py (in case you're using development server):
尝试将此添加到urls.py(如果您使用的是开发服务器):
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
)
#2
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.
将图像放在静态文件夹中将起作用,因为静态文件夹中的文件是在站点上提供的。这是一个潜在的解决方案,这个讨论似乎表明它对某些图像是合理的。在我的网站上,媒体文件夹中的图像位于/ uploads / <图像名称> 。这就是MEDIA_URL的目的。但是我从未明确地调用MEDIA_URL,因为我的所有图像都绑定到模型中,所以django会为我处理。我的猜测是仔细检查您存储图像的文件夹的名称,并确保您正在查看正确的位置。其次,我要确保模板中未定义{{MEDIA_URL}}。