媒体文件夹中的图像不显示 django 模板 [英] Images from media folder is not displaying django template
问题描述
我的媒体文件夹中有图像,我想显示它们,但 Django 不显示静态文件夹以外的图像.在我的设置中
I am having images in my media folder and I want to display them but Django is not displaying images other than static folder. In my setting
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR,'static'),]
MEDIA_ROOT = BASE_DIR
MEDIA_URL = '/media/'
在我的 urls.py 后的 url 模式中,我添加了这个
In my urls.py after url pattern I added this
+ static(MEDIA_URL, document_root=MEDIA_ROOT)
在我的模板中
<img class="card-img-top" style="" src="{{result.object.thumbnail.url}}" alt=""></a>
<p>{{result.object.thumbnail.url}}</p>
它显示了正确的路径但没有显示图像,我无法找出问题所在.谢谢
It is showing the correct path but not showing the image, I am unable to figure out the problem. Thanks
推荐答案
你的 MEDIA_ROOT
有你的项目根目录的路径.你必须加入它,你的 media
的目录.(我想 media/
是您上传所有媒体的目录名称)我认为您应该拥有这样的 MEDIA_ROOT
.
Your MEDIA_ROOT
have the path of the root of your project. You must join to it, the direcotry of your media
. (I suppose media/
is the directory name where you upload all your media)
I think you should have your MEDIA_ROOT
like that.
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
这篇关于媒体文件夹中的图像不显示 django 模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!