其他分享
首页 > 其他分享> > Django模板文件

Django模板文件

作者:互联网

def index(request):
    # return HttpResponse("欢迎来到 Django首页!")
    booklist = BookInfo.objects.all()
    template = loader.get_template('booktest/index.html')
    context = RequestContext(request, {'booklist': booklist})
    return HttpResponse(template.render(context))
————————————————
会报错如下
Exception Type:    TypeError
Exception Value:    
context must be a dict rather than RequestContext.

解决办法一:
def index(request):
    # return HttpResponse("欢迎来到 Django首页!")
    booklist = BookInfo.objects.all()
    template = loader.get_template('booktest/index.html')
    # context = RequestContext(request, {'booklist': booklist})
    context = {'booklist': booklist}
    return HttpResponse(template.render(context)
————————————————

解决办法二:

使用render函数代替

def index(request):

return render(request,'booktest/index.html',{'booklist': booklist})

标签:文件,return,context,index,request,Django,booklist,template,模板
来源: https://blog.csdn.net/senkkes/article/details/118276869