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


原代码:
# 使用模板文件
# 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)


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM