创建模板
在manage.py的同级目录创建templates文件夹
在templates目录下创建hello.html
编辑模板
<h1>{{hello}}</h1>
修改study/setting.py
修改 TEMPLATES 中的 DIRS 为 [BASE_DIR+"/templates",]
修改view.py
增加一个新的对象,用于向模板提交数据
数据与视图分离
这里使用 render 来替代之前使用的 HttpResponse。render 还使用了一个字典 context 作为参数。context 字典中元素的键值 "hello" 对应了模板中的变量 "{{ hello }}"。
# -*- coding: utf-8 -*-
#from django.http import HttpResponse
from django.shortcuts import render
def hello(request):
context = {}
context['hello'] = 'Hello World master!'
return render(request, 'hello.html', context)
如果服务还没关,刷新页面
image.png