前文中我們讓Django顯示了一段你想要文字,只是簡單的在views中通過HttpResponse返回的字符串,如果我想顯示一個(gè)頁面,難道還是繼續(xù)在views中寫頁面代碼嗎?當(dāng)然不是,我們需要使用templates。
1. app中使用template
繼續(xù)沿用上文中的代碼,在blog下新建一個(gè)名為templates的文件夾,在文件夾下新建一個(gè)index.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>我愛編程</title>
</head>
<body>
<h1>如果你看到這個(gè)頁面,說明調(diào)用文件成功</h1>
</body>
</html>
然后在blog下的views中重新定義index函數(shù)
from django.shortcuts import render
from django.http import HttpResponse
'''
def index(request):
return HttpResponse('天行健,君子自強(qiáng)不息!')
'''
def index(request):
return render(request, 'index.html')
此時(shí),在運(yùn)行服務(wù)器,訪問 127.0.0.1/blog/index/,是不是看到了H5頁面了呢。
注意:我在創(chuàng)建文件夾是命名為template時(shí),訪問頁面報(bào)錯(cuò),但是改為templates時(shí)則可以訪問到,這個(gè)我還沒找到原因,找到后會詳細(xì)說明。
在上面的views下的index函數(shù)中,我們使用了render,有興趣可以查一下這個(gè)函數(shù)的用法。如果想要顯示動態(tài)網(wǎng)頁,這里就要修改render:
from django.shortcuts import render
from django.http import HttpResponse
# def index(request):
# return HttpResponse('天行健,君子自強(qiáng)不息!')
def index(request):
return render(request, 'index.html', {'name': 'fuckCoding', 'age': '18'})
render函數(shù)的第三個(gè)參數(shù)是一個(gè)字典格式,第二個(gè)參數(shù)就是模板,整個(gè)函數(shù)就是將第三個(gè)參數(shù)傳遞的數(shù)據(jù)渲染到第二個(gè)參數(shù)對應(yīng)的模板上,然后返回給界面。
對應(yīng)的,我們需要修改index.html的內(nèi)容:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>我愛編程</title>
</head>
<body>
<h1>歡迎訪問我的{{ name }}界面,我今年{{ age }}歲</h1>
</body>
</html>
此時(shí)運(yùn)行,界面已經(jīng)將你想要顯示的數(shù)據(jù)渲染到界面了,這里特別要注意的是django模板以{{ }}方式傳遞參數(shù),但是外層兩個(gè)花括號間不能有空格,否則取不到數(shù)據(jù),不信你試試。
2. 多個(gè)app中使用template
上面我們講了如何在app中使用template,現(xiàn)在我們在程序中再創(chuàng)建一個(gè)名為notice的app,還記得怎么操作嗎?
python3 manage.py startapp notice
此時(shí)程序中生成了一個(gè)notice應(yīng)用,記得要在系統(tǒng)的settings.py下添加這個(gè)應(yīng)用:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
'notice',
]
我們在notice下新建一個(gè)文件夾templates,在下面新建一個(gè)index.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>user下index</title>
</head>
<body>
<h1>我是notice應(yīng)用下的index頁面</h1>
</body>
</html>
然后把blog下的blog_urls.py文件拷貝到notice下面,名字改為notice_urls.py,并修改系統(tǒng)urls.py下的內(nèi)容:
from django.conf.urls import url, include
from django.contrib import admin
from blog.views import index
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^blog/', include('blog.blog_urls')),
url(r'^notice/', include('notice.notice_urls')),
]
接著,再在notice下的views中定義一下index函數(shù),這次我們只返回一個(gè)靜態(tài)頁面
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request, 'index.html')
好了,我們大公告成了,是不是很開心。興奮滴打開 127.0.0.1:8000/blog/index,顯示如下界面:
這個(gè)是我們想要的內(nèi)容,完全沒錯(cuò)吧,接著我們再訪問127.0.0.1:8000/notice/index,如果返回“我是notice應(yīng)用下的index頁面”這么一句話,就證明我們成功了,但是結(jié)果返回的是:
這個(gè)界面和blog下的index顯示是一樣,只不過這里沒顯示對應(yīng)的參數(shù),因?yàn)槲覀冊趎otice的views中的index中只返回了兩個(gè)參數(shù),并沒有傳值。但是,但是為什么我們訪問的是notice下的index頁面,為何返回的還是blog下的index頁面呢?
這是因?yàn)椋覀冊赽log和notice下存在兩個(gè)同名的index文件,web服務(wù)器接收到前端訪問notice/index的請求時(shí),先去系統(tǒng)settings下讀取所有的app,
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
'notice',
]
按照順序,逐個(gè)app中去查詢有沒有名為index的文件,它會先找到blog下的index頁面,并返回,所以我們雖然訪問的是notice下的index,但是返回的仍然是blog下的index。有興趣你可以把settings下“blog”、“notice”的位置調(diào)換一下,你會發(fā)現(xiàn),你即使是訪問blog/index,界面顯示的仍然是notice下的index。
好了,我們知道問題所在,解決方法有兩個(gè):
一是,你保證你程序中所有app下所有的模版文件不會同名,這個(gè)維護(hù)起來是不是會很蛋疼?所以不推薦。
二是,在每個(gè)app的templates文件夾下再建一個(gè)與app名相同的文件夾,然后把對應(yīng)的模版文件都放在這個(gè)文件夾下,如下:
然后,分別修改blog、notice下views中index返回模版的路徑:
from django.shortcuts import render
from django.http import HttpResponse
# def index(request):
# return HttpResponse('天行健,君子自強(qiáng)不息!')
def index(request):
return render(request, 'blog/index.html', {'name': 'fuckCoding', 'age': '18'})
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return render(request, 'notice/index.html')
再訪問127.0.0.1:8000/notice/index/,就可以得到你想要的結(jié)果。