1、在項目根目錄下,創建templates目錄,在templates下新建index.html文件,PyCharm將自動生成html的文件內容格式。
<code> .
├── Book
├── BookManager
└── templates
└── index.html/<code>
2、編輯setting.py文件第58行,修改TEMPLATES內容如下,目的是添加templates路徑,好讓接下來程序能夠找到index.html文件。
<code> 'DIRS': [os.path.join(BASE_DIR,'templates')],/<code>
3、修改view.py文件如下。
<code> from django.shortcuts import render
from django.http import HttpResponse
def index(request):
context={
'H1':'OK!',
'H2':'-- By TeamsSix'
}
return render(request,'index.html',context)/<code>
4、修改index.html文件如下。
<code>
<title>Home/<title>
{{ H1 }}
{{ H2 }}
/<code>
5、此時,刷新瀏覽器。

6、視圖與templates的總結

閱讀更多 TeamsSix 的文章