简体   繁体   English

Django:如何在共享模板中添加静态文件?

[英]Django: How to add static files in shared templates?

I have shared templates that doesn't belongs to any particular app.我共享了不属于任何特定应用程序的模板。 Here's my project tree:这是我的项目树:

.
├── 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

In my settings file I have:在我的设置文件中,我有:

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')]

In my base.html template, I'm trying to call the styles.css from static/css/styles.css this way:在我的 base.html 模板中,我试图通过这种方式从 static/css/styles.css 调用styles.css:

{% load static %}
<link rel="stylesheet" type="text/css" href="{% static "css/styles.css" %}" />

But the server responds with a not found:但服务器响应未找到:

"GET /static/css/styles.css HTTP/1.1" 404

What I'm doing wrong?我做错了什么?

Seems like you're in development, so the DEBUG is True.似乎您正在开发中,因此 DEBUG 为 True。

STATICFILES_DIRS is the list of folders where Django will search for additional static files aside from the static folder of each app installed. STATICFILES_DIRS是 Django 将在其中搜索除安装的每个应用程序的静态文件夹之外的其他静态文件的文件夹列表。

Try to insert this list to your settings:尝试将此列表插入到您的设置中:

STATICFILES_DIRS = ['/home/path/to/your/static/',]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM