python - Django:如何在共享模板中添加静态文件?
问题描述
我有不属于任何特定应用程序的共享模板。这是我的项目树:
.
├── root_app
│ ├── asgi.py
│ ├── forms.py
│ ├── __init__.py
│ ├── __pycache__
│ ├── settings.py
│ ├── urls.py
│ ├── views.py
│ └── wsgi.py
├── db.sqlite3
├── another_app
│ ├── admin.py
│ ├── api_services.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ ├── models.py
│ ├── __pycache__
│ ├── templates
│ ├── tests.py
│ ├── urls.py
│ ├── utils.py
│ └── views.py
├── manage.py
├── Pipfile
├── Pipfile.lock
├── README.md
├── requirements.txt
├── static
│ └── css
│ └── styles.css
└── templates
├── base.html
└── home.html
在我的设置文件中,我有:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
INSTALLED_APPS = [
...
...
'django.contrib.staticfiles',
]
STATIC_URL = '/static/'
STATIC_DIR = [os.path.join(BASE_DIR, 'static')]
在我的 base.html 模板中,我试图以这种方式从 static/css/styles.css 调用 styles.css:
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static "css/styles.css" %}" />
但服务器响应未找到:
"GET /static/css/styles.css HTTP/1.1" 404
我做错了什么?
解决方案
好像您正在开发中,所以 DEBUG 是 True。
STATICFILES_DIRS
是 Django 将在其中搜索除每个已安装应用程序的静态文件夹之外的其他静态文件的文件夹列表。
尝试将此列表插入到您的设置中:
STATICFILES_DIRS = ['/home/path/to/your/static/',]
推荐阅读
- django - 如何在序列化器中使用序列化器?
- xaml - Xamarin - stacklayout数据模板中的绑定命令不起作用
- c++ - 暗网制造错误收集器2:错误:ld返回1退出状态
- python - 如何选择保留参考列熊猫数据框的列
- python - 离散正态分布以获得随机变量的概率
- python - 变量可以用作 PyMC3 模型中的“观察到的”数据吗?
- java - 如何使用二叉搜索比较树显示数组的值?
- python - HTTP请求函数,表示没有模型属性-DJango
- amazon-web-services - 我应该将我的对象存储在 AWS Running Ec2 Instance With Application Load Balancer 的什么位置
- python-3.x - Reportlab 中的表格内容太长