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})