【问题标题】:media image is not able to display on webpage媒体图像无法在网页上显示
【发布时间】:2022-01-08 16:20:11
【问题描述】:

当点击“哟”时,我可以看到媒体文件,但是当尝试通过图像标签显示图像时,在网页上看不到它

<a href="{{status.adv.url }}">yo</a>
<img src="{{status.adv.url}}" width="250" height="250"  alt="advertise"/>

【问题讨论】:

标签: django django-templates


【解决方案1】:

改变这个

<img src="{{status.adv.url}}" width="250" height="250"  alt="advertise"/>

<img src="{% static status.adv.url %}" width="250" height="250"  alt="advertise"/>

并在模板顶部包含{% load static %} 模板标签

你的项目 url.py 应该有这些

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)

文档:Managing static files (e.g. images, JavaScript, CSS)

【讨论】:

  • 图片上传到网站并存储在非静态媒体文件夹中
  • 是的,没关系,可以使用静态模板标签提供媒体,共享您的媒体文件设置和项目 url.py
  • @JAYESHrathi 查看更新后的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-24
  • 2014-09-27
  • 2015-07-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-12
  • 1970-01-01
相关资源
最近更新 更多