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