python - django1.11类型错误: context must be a dict rather than RequestContext

标签 python django

这是我的代码:

def contact(request):
    if request.method == 'POST':
        form = forms.ContactForm(request.POST)
        if form.is_valid():
            message = 'Thanks for your post.'
            user_name = form.cleaned_data['user_name']
            user_city = form.cleaned_data['user_city']
            user_school = form.cleaned_data['user_school']
            user_email = form.cleaned_data['user_email']
            user_message = form.cleaned_data['user_message']
            ctx = {
                'user_name':user_name,
                'user_city':user_city,
                'user_school':user_school,
                'user_email':user_email,
                'user_message':user_message,
                }
        else:
            message = 'Please check the format of your information.'
    else:
        form = forms.ContactForm()
    template = get_template('contact.html')
    request_context = RequestContext(request)
    request_context.push(locals())

    html = template.render(request_context)

    return HttpResponse(html)

但是我遇到了以下异常:

TypeError: context must be a dict rather than RequestContext.

如何修改代码以避免此错误? (我的英语很差,抱歉)

最佳答案

不推荐将 Context 对象传递给 Template.render()

尝试更改此设置:

request_context = RequestContext(request)
request_context.push(locals())

html = template.render(request_context)

对此:

html = template.render(context=locals(), request=request)

关于python - django1.11类型错误: context must be a dict rather than RequestContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45903767/

相关文章:

javascript - Django 和tinymce

python - 当 zip 文件在我的本地 GAE sdk 中正确读取时,什么会导致 Google App Engine 无法识别它

python - 类 httpresponse 结果为 405 - django

python - 如何获取 __init__.py 文件中导入的当前模块?

python - 如何检查瓦片 map 中的碰撞(Python/Pygame)

python - 我可以将 Django 模型对象作为单例获取吗?

Django 操作错误 : missing table; migration does not recognize missing table

python - Django 默认表的重要性

python - 根据两个元素对元组列表进行排序

python - 如何在 Pycharm 中调试