我怎麼可能會延長使用我的應用程序base.html
以下文件夾結構
文件夾結構
└───project
├───plots # <-- app
├───project
├───projects # <-- app
├───statiC# <-- project static files
│ ├───css
│ ├───html
│ └───img
└───users # <-- app
設置文件
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
和我通過{% load static %}
使用static
文件 - {% static '/css/base.css' %}
我也知道如何使用{% extends file %}
- {% extends users/html/base.html %}
我想從喜歡這樣{% extends 'html/base.html' %}
static
文件夾extend
,但是我無法找到一種如何實現這種關係的方式。
替代解決方案
我找到了一種替代的方式得到它的工作修改項目設置文件templates
條目。它的工作原理,但如果可能的話,我想保留所有靜態文件在一個地方。
文件夾結構
└───project
├───plots # <-- app
├───project
├───projects # <-- app
├───statiC# <-- project static files
│ ├───css
│ └───img
├───templates #<-- !now `base.html` is here!
└───users # <-- app
設置
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates')
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
模板加載器看起來有很大的區別在'DIRS'中列出的目錄中爲你的模板。所以,你可以把路徑放在'static'目錄,它應該可以工作。雖然,一般的Django慣例是將所有的html模板保存在一個名爲「templates」的目錄中。假設將來你會與其他Django開發者合作,這會導致混淆。 – xyres
我明白了,那麼你會建議我應該將'html'文件保存在'templates'文件夾中嗎?在加載模板時是否有一些'default'加載器專門用於此'folder'的存在? (我是Django的初學者)正在開發我的第一個項目。 –
我建議你從默認配置如此模板文件夾開始,在該文件夾內,你可以按照你的意願在子文件夾中組織你的HTML。同樣,因爲這是你的第一個項目,你可以看看https://docs.djangoproject.com/en/1.11/intro/tutorial01/ – iklinac