django - 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/'
解决方案
前段时间我遇到了同样的问题。应用这些更改解决了它。从 settings.py 中删除STATIC_DIR
和STATICFILES_DIRS
替换它们:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, '/static/'),
)
推荐阅读
- php - 在PHP中的日期数组中填充空日期
- go - 如何通过 HTTPS 在 Golang 中进行 SOAP 调用
- python - 用python将文本解析成段落(循环问题)
- google-apps-script - Exclude users from belonging to a particular group in G Suite
- pandas - 堆叠每日数据数据框以获得每小时输出
- view - 如何按字段对文档进行分组并使用带有 CouchDB 视图的自定义 reduce 函数列出不相等的值
- python - 我需要编写什么查询来获取表的某一行中的值
- processing - 未使用局部变量“img”的值
- r - 有人认识这种用于日期存储的表格吗?
- python - 在 Jupyter 中以颜色打印 f 字符串,并在混合中使用转义字符