1. 程式人生 > >django模版

django模版

https://code.ziqiangxuetang.com/django/django-template.html

在前面的幾節中我們都是用簡單的 django.http.HttpResponse 來把內容顯示到網頁上,本節將講解如何使用渲染模板的方法來顯示內容。

1. 建立一個 zqxt_tmpl 專案,和一個 名稱為 learn 的應用,並且

django-admin.py startproject zqxt_tmpl
cd zqxt_tmpl
python manage.py startapp learn

 

 

 

2. 把 learn 加入到 settings.INSTALLED_APPS中

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'learn',
)

3. 開啟 learn/views.py 寫一個首頁的檢視

from django.shortcuts import render

def home(request):
    return render(request, 'home.html')

4. 在 learn目錄下新建一個 templates 資料夾,裡面新建一個 home.html

預設配置下,Django 的模板系統會自動找到app下面的templates資料夾中的模板檔案。

目錄的結構是這樣的:

zqxt_tmpl
├── learn
│   ├── __init__.py
│   ├── admin.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── templates
│   │   └── home.html
│   ├── tests.py
│   └── views.py
├── manage.py
└── zqxt_tmpl
   ├── __init__.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

4 directories, 12 files

5. 在 home.html 中寫一些內容

<!DOCTYPE html>
<html>
<head>
    <title>歡迎光臨</title>
</head>
<body>
歡迎光臨自強學堂
</body>
</html>

 

 

6. 將檢視函式對應到網址,更改 zqxt_tmpl/urls.py

from django.conf.urls import include, url
from django.contrib import admin
from learn import views as learn_views
urlpatterns = [
    url(r'^$', learn_views.home, name='home'),
    url(r'^admin/', include(admin.site.urls)),
]

7. [可選] 建立資料庫表

1

2

3

4

python manage.py syncdb

 

# Django 1.7.x 以及上要用

python manage.py migrate

建立資料庫雖然本節不會用到,但是可以讓一些提示消失(提示你要建立資料庫之類的)

 

8. 執行開發伺服器,看看效果

python manage.py runserver

模板補充知識:

網站模板的設計,一般的,我們做網站有一些通用的部分,比如 導航,底部,訪問統計程式碼等等

nav.html, bottom.html, tongji.html

可以寫一個 base.html 來包含這些通用檔案(include)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<!DOCTYPE html>

<html>

<head>

    <title>{% block title %}預設標題{% endblock %} - 自強學堂</title>

</head>

<body>

 

{% include 'nav.html' %}

 

{% block content %}

<div>這裡是預設內容,所有繼承自這個模板的,如果不覆蓋就顯示這裡的預設內容。</div>

{% endblock %}

 

{% include 'bottom.html' %}

 

{% include 'tongji.html' %}

 

</body>

</html>

如果需要,寫足夠多的 block 以便繼承的模板可以重寫該部分,include 是包含其它檔案的內容,就是把一些網頁共用的部分拿出來,重複利用,改動的時候也方便一些,還可以把廣告程式碼放在一個單獨的html中,改動也方便一些,在用到的地方include進去。其它的頁面繼承自 base.html 就好了,繼承後的模板也可以在 block 塊中 include 其它的模板檔案。

比如我們的首頁 home.html,繼承或者說擴充套件(extends)原來的 base.html,可以簡單這樣寫,重寫部分程式碼(預設值的那一部分不用改)

1

2

3

4

5

6

7

8

{% extends 'base.html' %}

 

{% block title %}歡迎光臨首頁{% endblock %}

 

{% block content %}

{% include 'ad.html' %}

這裡是首頁,歡迎光臨

{% endblock %}

注意:模板一般放在app下的templates中,Django會自動去這個資料夾中找。但 假如我們每個app的templates中都有一個 index.html,當我們在views.py中使用的時候,直接寫一個 render(request, 'index.html'),Django 能不能找到當前 app 的 templates 資料夾中的 index.html 資料夾呢?(答案是不一定能,有可能找錯)

Django 模板查詢機制: Django 查詢模板的過程是在每個 app 的 templates 資料夾中找(而不只是當前 app 中的程式碼只在當前的 app 的 templates 資料夾中找)。各個 app 的 templates 形成一個資料夾列表,Django 遍歷這個列表,一個個資料夾進行查詢,當在某一個資料夾找到的時候就停止,所有的都遍歷完了還找不到指定的模板的時候就是 Template Not Found (過程類似於Python找包)。這樣設計有利當然也有弊,有利是的地方是一個app可以用另一個app的模板檔案,弊是有可能會找錯了。所以我們使用的時候在 templates 中建立一個 app 同名的資料夾,這樣就好了。

這就需要把每個app中的 templates 資料夾中再建一個 app 的名稱,僅和該app相關的模板放在 app/templates/app/ 目錄下面,

例如:專案 zqxt 有兩個 app,分別為 tutorial 和 tryit

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

zqxt

├── tutorial

│   ├── __init__.py

│   ├── admin.py

│   ├── models.py

│   ├── templates

│   │   └── tutorial

│   │       ├── index.html

│   │       └── search.html

│   ├── tests.py

│   └── views.py

├── tryit

│   ├── __init__.py

│   ├── admin.py

│   ├── models.py

│   ├── templates

│   │   └── tryit

│   │       ├── index.html

│   │       └── poll.html

│   ├── tests.py

│   └── views.py

├── manage.py

└── zqxt

    ├── __init__.py

    ├── settings.py

    ├── urls.py

    └── wsgi.py

這樣,使用的時候,模板就是 "tutorial/index.html" 和 "tryit/index.html" 這樣有app作為名稱的一部分,就不會混淆。