Html 在django模板中显示图像
我正在查询数据库中的图像并以HTML格式显示它们 我的观点是Html 在django模板中显示图像,html,django,Html,Django,我正在查询数据库中的图像并以HTML格式显示它们 我的观点是 @login_required def viewall(request): context = RequestContext(request) images = Images.objects.all().order_by('-id') return render_to_response('cms/view-all.html',{'images':images,'media_url':settings.MEDIA
@login_required
def viewall(request):
context = RequestContext(request)
images = Images.objects.all().order_by('-id')
return render_to_response('cms/view-all.html',{'images':images,'media_url':settings.MEDIA_URL},context)
我的view-all.html是
{% extends "cms/base.html" %}
{% block title %}
View-all
{% endblock %}
{% block main %}
{% for image in images %}
<img src="{{ media_url }}{{ image.file }}">
{% endfor %}
{% endblock %}
我的图像位于项目目录文件/media/images中
但是图像没有显示出来。我越来越像这样了
这里可能有什么问题?您需要将媒体url添加到项目的url模式中:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
请参阅详细信息。尝试此操作,您需要在路径之前添加“.”(假设媒体文件夹位于根目录中)
您可以通过检查图像来共享图像src路径吗?图像有2次。我怎么取一个?添加额外图像的确切位置可能在哪里?可能在image.file中,因为媒体url为/media/。检查你的数据库我应该在数据库中检查什么?好的,它起作用了。非常感谢你。如果你能看一看,我将不胜感激。
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
MEDIA_URL = './media/'