在使用模板时遇到的错误,TypeError at /index/ context must be a dict rather than RequestContext(亲测)
原代碼:
def index(request):
# 1.加載模板文件,獲取一個模板對象
temp = loader.get_template(‘booktest/index.html’)
# 2.定義模板上下文,給模板文件傳數據
context = RequestContext(request,{})
# 3.模板渲染,產生一個替換后的html內容
res_html = temp.render( contex)
# 4.返回應答
return HttpResponse(res_html)
更正后的代碼:
def index(request):
# 1.加載模板文件,獲取一個模板對象
temp = loader.get_template(‘booktest/index.html’)
# 2.定義模板上下文,給模板文件傳數據
context = RequestContext(request,{})
context.push(locals())
# 3.模板渲染,產生一個替換后的html內容
res_html = temp.render( context=locals())
# 4.返回應答
return HttpResponse(res_html)
————————————————
版權聲明:本文為CSDN博主「weixin_44201919」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/weixin_44201919/article/details/105843867
總結
以上是生活随笔為你收集整理的在使用模板时遇到的错误,TypeError at /index/ context must be a dict rather than RequestContext(亲测)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 钟睒睒成全球前十首位中国富豪 2021
- 下一篇: 股票投资需要注意的问题 考虑这几点就