在乙個網頁中,不僅僅只有乙個html
骨架,還需要css
樣式檔案,js
執行檔案以及一些等。因此在dtl
中載入靜態檔案是乙個必須要解決的問題。在dtl
中,使用static
標籤來載入靜態檔案。要使用static
標籤,首先需要。載入靜態檔案的步驟如下:
確保在settings.py
中設定了static_url
。
在模版中使用staticfiles_dirs =
[ os.path.join(base_dir,
"static"
)]
load
標籤載入static
標籤。比如要載入在專案的static
資料夾下的style.css
的檔案。那麼示例**如下:
rel=
"stylesheet"
href=""
>
如果不想每次在模版中載入靜態檔案都使用load
載入static
標籤,那麼可以在settings.py
中的templates/options
新增'builtins':['django.templatetags.static']
,這樣以後在模版中就可以直接使用static
標籤,而不用手動的load
了。
from django.conf import settings
from django.conf.urls.static import static
urlpatterns =
[# 其他的url對映
]+ static(settings.static_url, document_root=settings.static_root)
Django載入靜態檔案
1 安裝python和pip,這裡就不再贅述 2 通過pip 安裝命令安裝django,命令為 pip install django 3 進入命令列,進入你打算建立專案的位置,我專案安裝位置是 d python django 執行命令 django admin startproject hellow...
Django 模板載入靜態檔案
2 確保在 settings.py中設定了static url。staticfiles dirs os.path.join base dir,static 5 在模版中使用 load 標籤載入 static 標籤。比如要載入在專案的 static 資料夾下的 style.css 的檔案。那麼示例 如...
Django 中載入靜態檔案 static 詳解
2 確保在settings.py 中設定了static url 上面兩條都是在建立django專案的時候就自動給我們弄好了,只要我們沒有改動它,就不用管。如果不想每次在模板中載入靜態檔案都使用load載入static標籤,那麼可以在settings.py中的templates新增 builtins ...