Django1.11版本POST表单处理问题TypeError: context must be a dict rather than RequestContext.

hei_6775 发布于 2017/08/27 17:47
阅读 1K+
收藏 0

从Django更新到1.10后,context好像被弃用了,在下面的代码中在运行的过程中出现问题。

def contact(request):
    if request.method == 'POST':
        
        form = forms.ContactForm(request.POST)
        if form.is_valid():
            message = '感谢您的来信。'
            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 = "请检查您输入的信息是否正确"
    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)

在终端显示如下错误:

Traceback (most recent call last):
  File "/home/usual/VENV/local/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, inTraceback (most recent call last):
  File "/home/usual/VENV/local/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/home/usual/VENV/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/usual/VENV/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/usual/myweb/last/ch08www/mysite/views.py", line 89, in contact
    html = template.render(request_context)
  File "/home/usual/VENV/local/lib/python2.7/site-packages/django/template/backends/django.py", line 64, in render
    context = make_context(context, request, autoescape=self.backend.engine.autoescape)
  File "/home/usual/VENV/local/lib/python2.7/site-packages/django/template/context.py", line 287, in make_context
    raise TypeError('context must be a dict rather than %s.' % context.__class__.__name__)
TypeError: context must be a dict rather than RequestContext.
 inner
    response = get_response(request)
  File "/home/usual/VENV/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/usual/VENV/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/usual/myweb/last/ch08www/mysite/views.py", line 89, in contact
    html = template.render(request_context)
  File "/home/usual/VENV/local/lib/python2.7/site-packages/django/template/backends/django.py", line 64, in render
    context = make_context(context, request, autoescape=self.backend.engine.autoescape)
  File "/home/usual/VENV/local/lib/python2.7/site-packages/django/template/context.py", line 287, in make_context
    raise TypeError('context must be a dict rather than %s.' % context.__class__.__name__)
TypeError: context must be a dict rather than RequestContext.

主要就是TypeError: context must be a dict rather than RequestContext.

不知道该如何修改源代码才能解决该问题?(csrf已经添加进模版,setting.py里也加了),主要是django版本问题,1.11版本的话怎么修改源代码

加载中
0
Nathan416
Nathan416

typeerr 错误,调试过request_context的类型?

hei_6775
hei_6775
还是感谢
hei_6775
hei_6775
这个问题已经解决了,可以把 request_context = RequestContext(request) request_context.push(locals()) html = template.render(request_context) 修改为html = template.render(context=locals(),request=request)
返回顶部
顶部