Django系列——第一个Template

来源:互联网 发布:淘宝户外运动店铺 编辑:程序博客网 时间:2024/06/05 18:24

Template文件夹下包含了HTML文件,使用Django模板语言DTL,也可以使用第三方模板(jinja2)

TEMPLATES = [    {        'BACKEND': 'django.template.backends.django.DjangoTemplates',        }      ] 

步骤:

1、在App的模板下创建一个新的Template文件夹
2、在该文件夹下创建HTML文件
3、在views.py返回rander(),通过rander()把HTML页面返回给浏览器。

from django.shortcuts import renderdef index(request):    return render(request,'html_test.html')

DTL的使用

render()第三个参数,是一个dic类型

修改APP下views.py:

from django.shortcuts import renderdef index(request):    return render(request,'html_test.html',***{'django':'demo1'}***)

修改HTML

<h1>***{{django}}***</h1>#这里写键值,获取后台传递的数据

注意:
不同APP下Templates文件夹中的同名.html文件会造成冲突
解决:在APP的Templates文件夹下创建以APP名为名称的文件夹,将html文件放入其中。注意修改render()中的第二个参数

#html前加上目录return render(request, 'demo2/html_test.html')
原创粉丝点击