Anaconda Python 3.6
Django 1.11.8
def my_render(request, template_path, context_dict):
"""使用模板文件"""
# 之前的版本
# 1.加载模板文件
temp = loader.get_template(template_path)
# 2.定义模板上下文,给模板文件传递数据
context = RequestContext(request, context_dict)
# 3.模板渲染,产生标准的html内容
res_html = temp.render(context)
# 4.返回数据给浏览器
return HttpResponse(res_html)
def index(request):
return my_render(request, 'text1/index.html', {'content': 'hello'}
分隔线,上边是1.11之前的写法,在1.11之后不适用,应该改成
# 1.加载模板文件
temp = loader.get_template(template_path)
# 2.模板渲染,产生标准的html内容,直接以字典的形式传入数据
res_html = temp.render(context_dict)
# 3.返回数据给浏览器
return HttpResponse(res_html)