django的static配置查了不少文章,都是複製貼上的,都沒搞定,後來沒辦法,只能去官網查英文文件。
雖然英文不行,但是能看懂基本的配置資訊。照著官方的配置,果然成功了。大致配置如下:
修改setting.py,後面加入
static_url ='/static/'上面那句是寫死的,讓django能呼叫靜態檔案,下面這句是配置靜態檔案的路徑,路徑可以填寫多個,用逗號隔開staticfiles_dirs = (
os.path.join(base_dir, "static"),
)base_dir一般是django定義好的,在setting.py的最上面:
base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
os.path.join的作用是把兩個路徑拼接成乙個完整的路徑。
上面配置修改完,還需要在urls.py中加入配置,首先要引入模組
from django.conf.urls.static import staticfrom django.conf import settings
至此,django就可以呼叫靜態檔案了,模版檔案還要注意寫法
,不要忘記寫
html
>
<
html
lang=
"en"
>
<
head
>
<
meta
charset=
"utf-8"
>
<
title
>title
title
>
<
link
ref=
"stylesheet"
href=""
/>
head
>
Django1 9學習筆記
等 django1.9的templates系統和1.7有些許不一樣,原來的templates系統要由使用者自己定義。如 template path os.path.join base dir,templates template dirs put strings here,like home htm...
Django 1 9 靜態檔案配置
django 靜態檔案的配置涉及到settings 中的三個配置項static url staticfiles dirs及static root 1.開發伺服器中靜態檔案的配置及應用 1 static url 配置 預設情況下 settings 配置 static url static 2 stat...
Django 1 9 開發web專案
django.contrib.admin django.contrib.auth django.contrib.contenttypes django.contrib.sessions django.contrib.messages django.contrib.staticfiles rest f...