【问题标题】:Django Full image is not loadingDjango完整图像未加载
【发布时间】:2021-01-23 16:16:46
【问题描述】:

我正在使用 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/'

【问题讨论】:

  • 尝试添加STATIC_ROOT = STATIC_DIR
  • ViLuWi:当我添加此 STATIC_ROOT = STATIC_DIR 时,出现以下错误。 ERRORS: ?: (staticfiles.E002) STATICFILES_DIRS 设置不应包含 STATIC_ROOT 设置。 Eric Martin:我尝试了您的建议,但结果没有任何变化。

标签: django static


【解决方案1】:

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

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

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

【讨论】:

  • 我尝试了你的建议。我无法让它工作。还有别的吗?底部有STATIC_URL = '/static/' 吗?感谢 cmets。
【解决方案2】:

所以问题最终是图像 html 的包装方式。它必须是一行,如下所示。

<img src="{% static 'images/rango.jpg' %}"alt="Picture of Rango" />

【讨论】:

    猜你喜欢
    • 2019-07-24
    • 2012-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-07
    • 1970-01-01
    • 2014-04-29
    相关资源
    最近更新 更多