- django-2 模板文件的加载
- 1.创建模板文件 index.html
- 2.在django使用模板文件 需要在app下创建templates文件夹 此为django固定模板文件位置
- 3.编辑views.py文件
- 添加index.html文件的使用方法
- from django,template import loader,Context
- def index(req):
- t=loader.get_template('index.html') #生成模板对象
- c=Context({}) # 生成context对象
- return HttpResponse(t.render(c)) # 调用返回对象
- 此步骤是一步一步增加的,先增加调用模板的方法,在给模板渲染对象,此处对象为空,最后返回对象
- 4.启动开发服务器 runserver 进行测试
- 5.更简单的模板写法如下
- from django.shortcuts import render_to_response
- def index(req):
- return render_to_response('index.html',{})
转载于:https://blog.51cto.com/kkkkkk/1177668