首页 文章

Django 1.11上的错误“TypeError context必须是dict而不是Context”

提问于
浏览
0

我收到一条消息TypeError上下文必须是dict而不是Context . 但不知道如何解决它 .

def comment(request,id):
    if id:
        r = Restaurant.objects.get(id=id)
    else:
        return HttpResponseRedirect("/restaurants_list/")
    if request.POST:
        visitor = request.POST['visitor']
        content = request.POST['content']
        email = request.POST['email']
        date_time = timezone.localtime(timezone())
        Comment.objects.create(
            visitor=visitor,
            email=email,
            content=content,
            date_time=date_time,
            restaurant=r
        )
    return render_to_response('comments.html', RequestContext(request, locals()))

1 回答

  • 0

    你可以看到context论证的解释:

    要添加到模板上下文的值的字典 . 默认情况下,这是一个空字典 . 如果字典中的值是可调用的,则视图将在呈现模板之前调用它 .

    更改

    return render_to_response('comments.html', RequestContext(request, locals()))
    

    return render_to_response('comments.html', context=locals())
    

相关问题