【问题标题】:cannot load image in html template in django application无法在 django 应用程序的 html 模板中加载图像
【发布时间】:2017-11-09 06:43:33
【问题描述】:

我试图在我的 django 应用程序中的 html 模板中放置一个图像,但它没有显示。当我检查时,它说图像无法加载。

HTML 代码:

<div class = "col-md-4">
  <img src= "/static/images/abc.jpg" alt="sorry"/>
  </div>

图片 abc.png 的位置是 /home/user/demo/mysite/mysite/static/images/abc.png 我的 django 应用程序是位于 mysite(demo/mysite) 中的书签

这是在 django 应用程序中提供路径的正确方法还是需要做其他事情。

.
└── mysite
    ├── bookmark
    │   ├── admin.py
    │   ├── admin.pyc
    │   ├── apps.py
    │   ├── forms.py
    │   ├── forms.pyc
    │   ├── __init__.py
    │   ├── __init__.pyc
    │   ├── migrations
    │   │   ├── 0001_initial.py
    │   │   ├── 0001_initial.pyc
    │   │   ├── 0002_auto_20171029_1436.py
    │   │   ├── 0002_auto_20171029_1436.pyc
    │   │   ├── 0003_userbookmark.py
    │   │   ├── 0003_userbookmark.pyc
    │   │   ├── __init__.py
    │   │   └── __init__.pyc
    │   ├── models.py
    │   ├── models.pyc
    │   ├── static
    │   │   └── style.css
    │   ├── templates
    │   │   ├── 20150225_134637.jpg
    │   │   ├── abc.jpg
    │   │   ├── base_generic.html
    │   │   ├── category.html
    │   │   ├── index.html
    │   │   ├── login.html
    │   │   ├── myprofile.html
    │   │   ├── profile.html
    │   │   ├── register2.html
    │   │   ├── register.html
    │   │   ├── registerInterest.html
    │   │   ├── sample
    │   │   └── success.html
    │   ├── tests.py
    │   ├── urls.py
    │   ├── urls.pyc
    │   ├── views.py
    │   └── views.pyc
    ├── db.sqlite3
    ├── manage.py
    ├── mysite
    │   ├── __init__.py
    │   ├── __init__.pyc
    │   ├── settings.py
    │   ├── settings.pyc
    │   ├── static
    │   │   └── images
    │   │       ├── abc.jpg
    │   │       └── icons_core-88916797fd4fde57d4120dea4c239f1d@1x.png
    │   ├── urls.py
    │   ├── urls.pyc
    │   ├── wsgi.py
    │   └── wsgi.pyc
    └── urls.py

我已经编辑了帖子并添加了我的项目的树结构

【问题讨论】:

标签: html django


【解决方案1】:

这是在 django 模板中使用静态文件的正确方法

{% load static %}

<div class="col-md-4">
    <img src= "{% static "images/abc.jpg" %} alt="sorry"/>
</div>

【讨论】:

  • 我也试过这个,但没有奏效。我们是否也需要更改 settings.py 文件?
猜你喜欢
  • 2017-08-01
  • 1970-01-01
  • 2013-01-09
  • 1970-01-01
  • 2016-06-04
  • 2017-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多