将django从1.6升级到1.7后的ValueError

时间:2014-09-27 07:39:43

标签: django django-views django-1.7

从Django 1.6.5升级到1.7后,我的视图会抛出以下错误:

ValueError at /mrp/manage/materials/ dictionary update sequence element #0 has length 12; 2 is required

断点位于我在视图函数中返回的render函数:

def manage_materials(request):
    context = dict()
    context['material_list'] = Material.objects.order_by('material_type', 'name')
    return render(request, 'erp/mrp/manage_materials.html',
                  RequestContext(request, context)) 

在我的代码中似乎是一般错误,render上的每次调用都会触发此错误。


FULL STACKTRACE

Environment:


Request Method: GET
Request URL: http://localhost:8000/mrp/manage/materials/

Django Version: 1.7
Python Version: 3.4.1
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'erp')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "C:\Python34\lib\site-packages\django\core\handlers\base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python34\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
  22.                 return view_func(request, *args, **kwargs)
File "D:\app\ecerp\erp\views\mrp.py" in manage_materials
  125.                   RequestContext(request, context))
File "C:\Python34\lib\site-packages\django\shortcuts.py" in render
  48.     return HttpResponse(loader.render_to_string(*args, **kwargs),
File "C:\Python34\lib\site-packages\django\template\loader.py" in render_to_string
  177.     with context_instance.push(dictionary):
File "C:\Python34\lib\site-packages\django\template\context.py" in push
  54.         return ContextDict(self, *args, **kwargs)
File "C:\Python34\lib\site-packages\django\template\context.py" in __init__
  19.         super(ContextDict, self).__init__(*args, **kwargs)

Exception Type: ValueError at /mrp/manage/materials/
Exception Value: dictionary update sequence element #0 has length 12; 2 is required

有谁知道为什么?

1 个答案:

答案 0 :(得分:3)

这是因为render()快捷方式会自动在上下文中应用RequestContext。在django 1.6中,打电话是合法的:

def myview(request):
    context = {'my_var': 'my_value'}
    return render(request, 'erp/mrp/manage_materials.html',
              RequestContext(request, context)) 

其中RequestContext作为render()函数的第三个参数。

事实上这并不需要(参见request对象在那里传递两次)。 render()快捷方式函数将创建一个RequestContext对象,该对象可以从请求中获取所有上下文。

因此,有两种方法可以解决此错误:

  1. 显式设置第三个参数RequestContet(...)的名称为context_instance

    返回渲染(请求,'erp / mrp / manage_materials.html',               context_instance = RequestContext(request,context))

  2. 只需使用dict上下文作为第三个参数:

    返回渲染(请求,'erp / mrp / manage_materials.html',上下文)

  3. 第二个更好。


    注意:

    关于render()快捷方式功能的文档之间没有区别:

      

    1.7 doc:https://docs.djangoproject.com/en/1.7/topics/http/shortcuts/#render

         

    1.6 doc:https://docs.djangoproject.com/en/1.6/topics/http/shortcuts/#render