[英]Django: How to add static files in shared templates?
我共享了不属于任何特定应用程序的模板。 这是我的项目树:
.
├── 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/',]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.