Alfred Huang
Alfred Huang

Reputation: 18235

ValueError after upgrading django from 1.6 to 1.7

After I upgrade from Django 1.6.5 to 1.7, my view throws the error below:

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

And the break point is on the render function I returned in my view function:

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)) 

It seemed a general error throughout my code, every call on render trigger this error.


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

Do anyone knows why?

Upvotes: 1

Views: 657

Answers (1)

Alfred Huang
Alfred Huang

Reputation: 18235

It's because the render() shortcuts will automatically apply RequestContext on the context. In django 1.6, it is legal to call:

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

Which takes a RequestContext as the third parameter of render() function.

And this is no need in fact (see the request object is passed twice there). And the render() shortcut function will create a RequestContext object, which can take the context from the request all around.

So, there is two solution to fix this error:

  1. explicit set the name of the third parameter RequestContet(...) to context_instance:

    return render(request, 'erp/mrp/manage_materials.html', context_instance=RequestContext(request, context))

  2. just use a dict context as the third parameter:

    return render(request, 'erp/mrp/manage_materials.html', context)

The second one is nicer.


NOTE:

There is no difference between the docs about the render() shortcut function:

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

Upvotes: 3

Related Questions