在 HelloWorld 目录底下创建 templates 目录并建立hello.html文件,整个目录结构如下:
hello.html 文件代码如下:
修改HelloWorld/settings.py,修改 TEMPLATES中的 DIRS 为[BASE_DIR+"/templates",],如下所示:
修改view.py,增加一个新的对象,用于向模板提交数据:
render替代HttpResponse。render还使用了一个字典context作为参数。
context 字典中元素的键值 "hello"对应了模板中的变量 "{{ hello }}"。
再访问http://127.0.0.1:8000/hello/,可以看到页面: