首页 > 解决方案 > Django图像标签不显示照片

问题描述

我想通过在 Django 模板文件“index.html”中使用 img src 标签来显示图像:

templates/
    index.html
static/ 
    img/
       puzzle.png

将图像添加到静态后,我收集了静态并在页面顶部添加了:

{% load static %}

我的图像标签是:

<img src="{% static 'img/puzzle.png' %}">

但图像没有显示在我的页面上。我怎样才能解决这个问题?

标签: djangoimagestaticdjango-viewssrc

解决方案


如下更改您的目录结构,

yourapp/
├── templates/
│   └── index.html
└── static/
    └── yourapp
        └── puzzle.png

然后在模板中,

{% load static %}
<img src="{% static 'yourapp/puzzle.png' %}">


参考
配置静态文件


推荐阅读