Django加载静态标签不显示图像

问题描述 投票:0回答:1

尝试在我的浏览器上查看图像,但我只能看到图像占位符。

这是模板代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
    <body>
        <form action="" method="POST" style="background-color: rgb(201, 201, 24); padding: 10px;">
            {% csrf_token %}
        
            {{ form.as_p }}
       
            <input type="submit" value="submit">
    
        </form>


        {% load static %}
        <img src="{% static 'myapp/mini.jpg}" alt="My image"/>
    
    
    </body>
</html>

在我的设置中:

STATIC_URL = 'static/'

STATICFILES_DIRS = [
    'myapp/static',

]

STATIC_ROOT = 'static/'

可能是什么问题?

我尝试重命名文件夹并重新排列图像位置,但没有帮助。

html django django-templates
1个回答
0
投票

您的静态标签未关闭,而且,

STATIC_ROOT
必须是
collectstatic
可以将所有静态文件复制到的目录的绝对路径。

所以通常你会有设置;

STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")

关闭您的标签;

        {% load static %}
        <img src="{% static 'myapp/mini.jpg' %}" alt="My image"/>
© www.soinside.com 2019 - 2024. All rights reserved.