Django模板应用实例templates\hello.html

来源:互联网 发布:江苏省软件考试 编辑:程序博客网 时间:2024/06/06 17:00

HelloWorld 目录底下创建 templates 目录并建立hello.html文件,整个目录结构如下:



hello.html 文件代码如下:


修改HelloWorld/settings.py,修改 TEMPLATES中的 DIRS[BASE_DIR+"/templates",],如下所示:



修改view.py,增加一个新的对象,用于向模板提交数据:



render替代HttpResponserender还使用了一个字典context作为参数。

context 字典中元素的键值 "hello"对应了模板中的变量 "{{ hello }}"

再访问http://127.0.0.1:8000/hello/,可以看到页面:



0 0