码迷,mamicode.com
首页 > 其他好文 > 详细

在Django中template遇到 "context must be a dict rather

时间:2019-11-20 18:04:35      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:family   python   http   value   遇到   highlight   渲染   must   ror   

原代码:
# 使用模板文件
# 1.加载模板文件,获取一个模板文件
temp = loader.get_template(‘booktest/index.html‘)

# 2.定义模板上下文:给模板文件传递数据
context = RequestContext(request, {})

# 3.模板渲染:产生标准的html内容
res_html = temp.render(context)

# 4.返回给浏览器
return HttpResponse(res_html)

  

错误提示:

Exception Type: TypeError at /index
Exception Value: context must be a dict rather than RequestContext.

正确代码:

# 1.加载模板文件
temp = loader.get_template(‘booktest/index.html‘)

# 2.定义模板上下文:给模板文件传递数据
context =  RequestContext(request, {})
context.push(locals())

# 3.模板渲染:产生标准的html内容
res_html = temp.render(context=locals(), request=request)

# 4.返回给浏览器
return HttpResponse(res_html)

  

原代码:
# 使用模板文件
# 1.加载模板文件
temp = loader.get_template(‘booktest/index.html‘)

# 2.定义模板上下文:给模板文件传递数据
context = RequestContext(request, {})

# 3.模板渲染:产生标准的html内容
res_html = temp.render(context)

# 4.返回给浏览器
return HttpResponse(res_html)

错误提示:

Exception Type: TypeError at /index
Exception Value: context must be a dict rather than RequestContext.

正确代码:

# 1.加载模板文件
temp = loader.get_template(‘booktest/index.html‘)

# 2.定义模板上下文:给模板文件传递数据
context =  RequestContext(request, {})
context.push(locals())

# 3.模板渲染:产生标准的html内容
res_html = temp.render(context=locals(), request=request)

# 4.返回给浏览器
return HttpResponse(res_html)

在Django中template遇到 "context must be a dict rather

标签:family   python   http   value   遇到   highlight   渲染   must   ror   

原文地址:https://www.cnblogs.com/mibao/p/11899462.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!