【问题标题】:showing image in templates using django使用 django 在模板中显示图像
【发布时间】:2014-06-10 08:52:45
【问题描述】:

我是 django 的新手。我的项目文件夹包含媒体文件夹。它包含一些图像。

如何在其他应用的模板中显示此图像?

路径:mysite/media/blog/templates/blog/details.html

details.html

{{<img src="media/image_2.jpg">}}

但它没有显示任何东西..

【问题讨论】:

  • 在我的 details.html 页面中我正在尝试
  • 为什么要把它放在模板标签中?
  • 这是一段硬编码的 HTML。为什么你认为你必须把它放在任何东西里?只需将其放在您的模板中即可。
  • 实际上我正在尝试显示通过管理员上传的图像..我正在尝试此代码: 但它没用..所以我曾经尝试过第一个..

标签: python django


【解决方案1】:

您应该设置 STATIC 文件夹并配置媒体文件。

完整参考:https://docs.djangoproject.com/en/dev/howto/static-files/ 以及 https://docs.djangoproject.com/en/1.4/howto/static-files/.

但是你应该使用这样的东西(一旦你正确配置了 STATIC 文件):

<img src="{% static "media/myexample.jpg" %}"/>

编辑: 在使用static 标签之前,您必须加载特定标签,包括模板代码顶部:{% load staticfiles %}Referring to static files in templates

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-16
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 1970-01-01
    • 2015-06-11
    相关资源
    最近更新 更多