1、在项目根目录下,创建templates目录,在templates下新建index.html文件,PyCharm将自动生成html的文件内容格式。

.
├── Book
├── BookManager
└── templates
    └── index.html

2、编辑setting.py文件第58行,修改TEMPLATES内容如下,目的是添加templates路径,好让接下来程序能够找到index.html文件。

'DIRS': [os.path.join(BASE_DIR,'templates')],

3、修改view.py文件如下。

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)

4、修改index.html文件如下。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Home</title>
</head>
<body>
<H1 style="color:red;">{{ H1 }}</H1>
<H2 style="color:limegreen;">{{ H2 }}</H2>
</body>
</html>

5、此时,刷新浏览器。

【Django 学习笔记】4、模板_django

6、视图与templates的总结

【Django 学习笔记】4、模板_html_02

更多信息欢迎关注我的微信公众号:TeamsSix

【Django 学习笔记】4、模板_微信公众号_03