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

Error “TypeError context must be a dict rather than Context” on Django 1.11

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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()))

你可以看到context参数的描述:

A dictionary of values to add to the template context. By default, this is an empty dictionary. If a value in the dictionary is callable, the view will call it just before rendering the template.

改变你的行:

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

至:

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