1,為什么要有模板
從一個簡單的例子開始,我們要網(wǎng)頁上顯示當前時間,視圖的中函數(shù)如下:
def current_datetime(request):
now = datetime.datetime.now()
html = "<html><body>It is now %s.</body></html>" % now
return HttpResponse(html)
很顯然這段代碼看起來有點怪
使用模板大致有以下幾個優(yōu)點:
- 將業(yè)務邏輯的Python代碼和頁面設計的HTML代碼分離
- 使代碼更干凈整潔更容易維護
- 使Python程序員和HTML/CSS程序員分開協(xié)作尖滚,提高生產(chǎn)的效率
- 將HTML代碼分離出來彤叉,使其能夠復用
2,Django中怎么使用模板
當我們新建完project狈究,新建完app俘枫,設置完settings.py和urls.py后钟病,我把HTML文件放在哪里地方返吻,也就是templates目錄應該放在哪個地方姑子,通常有兩種方式:
- 在應用的目錄下新建templates
- 在工程的目錄下新建templates
3,應用下新建templates
這是Django默認的方式,意思就是一個應用會自動到本應用目錄下的templates目錄搜索html文件测僵,或者說html文件對本應用的視圖函數(shù)是透明可見的街佑,不需要再去在settings.py設置TEMPLATE_DIRS,Django 1.8以上的版本做了顯示的說明
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
},
]
例如我的工程是website谢翎,應用是blog,目錄結構如下:
fage:website$ tree
.
├── blog
│ ├── admin.py
│ ├── admin.pyc
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── migrations
│ │ ├── __init__.py
│ │ └── __init__.pyc
│ ├── models.py
│ ├── models.pyc
│ ├── templates
│ │ └── index.html
│ ├── tests.py
│ ├── urls.py
│ ├── urls.pyc
│ ├── views.py
│ └── views.pyc
├── db.sqlite3
├── manage.py
└── website
├── __init__.py
├── __init__.pyc
├── settings.py
├── settings.pyc
├── urls.py
├── urls.pyc
├── wsgi.py
└── wsgi.pyc
views.py視圖函數(shù)如下:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def blog_index(request):
content = {
'test' : 'just for test.',
'welcome': 'hello word'
}
return render(request, 'index.html', content)
index.html文件如下:
<p> {{ test }}</p>
<p> {{ welcome }} </p>
運行web服務器沐旨,就能在網(wǎng)頁上顯示對應的內(nèi)容
在上面的基礎上森逮,通常還在temlates目錄下新建一個同名app(名字可以不一樣)的目錄,主要將基礎模板文件base.html放在外層
目錄結構如下:
fage:blog$ tree templates/
templates/
├── base.html
└── blog
└── index.html
1 directory, 2 files
之前的目錄結構是
fage:blog$ tree templates/
templates/
└── index.html
0 directories, 1 file
fage:blog$
這時就要將views.py文件稍作修改:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def blog_index(request):
content = {
'test' : 'just for test.',
'welcome': 'hello word'
}
return render(request, 'blog/index.html', content)
總之一句話,app的views.py能夠自動識別當前所有目錄下templates目錄下的文件磁携,不要出現(xiàn)app的templates目錄下出現(xiàn)同名文件褒侧,否則,有的不能識別谊迄,但不能遞歸識別闷供,需要加上子目錄的名字。
templates設置的相關參考:
VASKS
知乎
4,工程目錄下設置templates
這種方式也比較常見统诺,但是需要在settings.py中指定目錄歪脏,不要忘了后面的逗號
TEMPLATE_DIRS = (
os.path.join(BASE_DIR,'templates'),
)
當然使用絕對路徑也可以,只不過靈活性差一些粮呢,當別人使用你的工程時婿失,需要做一些調(diào)整,因為目錄結構不一樣
TEMPLATE_DIRS = (
'/home/django/website1/templates',
)
這樣當我們新建多個app的時候啄寡,/home/django/website1/templates目錄下的html文件對任何一個視圖函數(shù)來說都是透明的豪硅,例如:
fage:website1$ tree
.
├── account
│ ├── admin.py
│ ├── __init__.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── blog
│ ├── admin.py
│ ├── admin.pyc
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── migrations
│ │ ├── __init__.py
│ │ └── __init__.pyc
│ ├── models.py
│ ├── models.pyc
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── db.sqlite3
├── manage.py
├── templates
│ ├── article.html
│ ├── base.html
│ └── login.html
└── website1
├── __init__.py
├── __init__.pyc
├── settings.py
├── settings.pyc
├── urls.py
├── urls.pyc
├── wsgi.py
└── wsgi.pyc
這是temlates目錄下的三個html文件對account和blog應用來說都是可見的,從三個文件來看挺物,我們就知道base.html文件是基礎模板舟误,是account和blog都要用的文件,而login.html文件是account應用對應視圖要用到的文件姻乓,article.html是blog應用對應視圖要用的文件嵌溢,在上面的基礎上,通常我還需要對templates目錄的文件結構做一次調(diào)整蹋岩,新建blog目錄赖草,新建account目錄
fage:website1$ tree templates/
templates/
├── account
│ └── article.html
├── base.html
└── blog
└── login.html
2 directories, 3 files