首页 > 解决方案 > Django完整图像未加载

问题描述

我正在用 Django 教程做 Tango。我正在使用 Django 版本 3.1.1。我能够让我的图像加载到本地链接“http://127.0.0.1:8000/static/images/rango.jpg”

我没有让它加载到 index.html 代码是

<!DOCTYPE html>
{% load static %} 
<html>
    <head>
        <title> Rango</title>
    </head>
    <body>
        <h1>Rango says...</h1>
        <div>
            hey there partner! <br />
            <strong>{{ boldmessage }} </strong><br />
        </div>
        <div>
            <a href="/rango/about/">About</a><br />
            <img src="{% static
                'images/rango.jpg' %}" 
                alt="Picture of Rango" /> 
        </div>
    </body>
</html>

我确实尝试删除 html 中的“alt=”,但这没有用。
我不认为问题出在我的 settings.py 上,但无论如何我都会分享 settings.py 的某些部分。

TEMPLATE_DIR = os.path.join(BASE_DIR,'templates') # this is the file path for templates
STATIC_DIR = os.path.join(BASE_DIR,'static') # this is the file path for static
STATICFILES_DIRS = [STATIC_DIR,]
...

#At the very bottom...
STATIC_URL = '/static/'

标签: djangostatic

解决方案


前段时间我遇到了同样的问题。应用这些更改解决了它。从 settings.py 中删除STATIC_DIRSTATICFILES_DIRS替换它们:

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

STATICFILES_DIRS = (
  os.path.join(BASE_DIR, '/static/'),
)

推荐阅读