簡體   English   中英

Django:如何在共享模板中添加靜態文件?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM