Python 字典更新序列元素#0的长度为15;2是必需的
我正在将python/django应用程序从1.6.5升级到1.7。我在解决以下错误时遇到问题:dictionary update sequence元素#0的长度为15;2是必需的 以下是回溯输出:Python 字典更新序列元素#0的长度为15;2是必需的,python,django,python-2.7,sequence,django-1.7,Python,Django,Python 2.7,Sequence,Django 1.7,我正在将python/django应用程序从1.6.5升级到1.7。我在解决以下错误时遇到问题:dictionary update sequence元素#0的长度为15;2是必需的 以下是回溯输出: Request Method: GET Request URL: http://127.0.0.1:8000/dashboard/ Django Version: 1.7 Python Version: 2.7.5 Installed Applications: ('django.contrib.
Request Method: GET
Request URL: http://127.0.0.1:8000/dashboard/
Django Version: 1.7
Python Version: 2.7.5
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'django.contrib.humanize',
'bootstrap3',
'ajax_select',
'appconf',
'versiontools',
'compressor',
'googlecharts',
'django_extensions',
'mandala',
'locations',
'statistics',
'alarms',
'accounts',
'assets')
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 "/Users/CLDSupportSystems/mandala-system/mandala_env_django_1.7/lib/python2.7/sit-packages/django/core/handlers/base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/CLDSupportSystems/mandala-system/mandala_env_django_1.7/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
22. return view_func(request, *args, **kwargs)
File "/Users/CLDSupportSystems/mandala-system/mandala_env_django_1.7/mandala/mandala/views.py" in dashboard
117. return render_to_response('dashboard/dashboard.html', variables,context_instance=RequestContext(request))
File "/Users/CLDSupportSystems/mandala-system/mandala_env_django_1.7/lib/python2.7/site-packages/django/shortcuts.py" in render_to_response
23. return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "/Users/CLDSupportSystems/mandala-system/mandala_env_django_1.7/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
177. with context_instance.push(dictionary):
File "/Users/CLDSupportSystems/mandala-system/mandala_env_django_1.7/lib/python2.7/site-packages/django/template/context.py" in push
54. return ContextDict(self, *args, **kwargs)
File "/Users/CLDSupportSystems/mandala-system/mandala_env_django_1.7/lib/python2.7/site-packages/django/template/context.py" in __init__
19. super(ContextDict, self).__init__(*args, **kwargs)
Exception Type: ValueError at /dashboard/
Exception Value: dictionary update sequence element #0 has length 15; 2 is required
将在以下行引发错误:
return render_to_response('dashboard/dashboard.html', variables,context_instance=RequestContext(request))
以下是变量的定义:
variables = RequestContext(request, {
'fresh_locations': fresh_data_locations,
'webalert_locations': webalert_locations,
'locations_reporting': reporting_locations,
'locations_not_reporting': locations_not_reporting,
'inalarm_count': inalarm_count,
'inalarm_stores': inalarm_stores_qs,
'workspace_count': workspace_count,
'user_profile': user_profile,
})
有人能给我指出正确的方向吗?render\u to\u response()的第二个参数必须是字典。您正在传入一个
RequestContext()
删除RequestContext()
对象,并将变量设置为一个字典:
variables = {
'fresh_locations': fresh_data_locations,
'webalert_locations': webalert_locations,
'locations_reporting': reporting_locations,
'locations_not_reporting': locations_not_reporting,
'inalarm_count': inalarm_count,
'inalarm_stores': inalarm_stores_qs,
'workspace_count': workspace_count,
'user_profile': user_profile,
}
这是因为render_to_response()和render()快捷方式将自动对上下文变量(变量目录)应用RequestContext。这肯定是一个打字错误,但为什么在你问之前它就起作用了
它可以使用到1.6版本,但是django.template.context中的实现RequestContext在1.7版本中发生了更改。它被分解为更面向对象和优化,以提高效率。历史就在这里,我想是的
此提交特定于优化的RequestContext:
以下是如何重新生成错误:
>>> from django.template import RequestContext
>>> from django.test.client import RequestFactory
>>> request_factory = RequestFactory()
>>> request = request_factory.get('www.google.com')
>>> fresh_data_locations, webalert_locations, reporting_locations, locations_not_reporting, inalarm_count, inalarm_stores_qs, workspace_count, user_profile = '', '', '', '', '', '', '', ''
>>> variables = RequestContext(request, {
... 'fresh_locations': fresh_data_locations,
... 'webalert_locations': webalert_locations,
... 'locations_reporting': reporting_locations,
... 'locations_not_reporting': locations_not_reporting,
... 'inalarm_count': inalarm_count,
... 'inalarm_stores': inalarm_stores_qs,
... 'workspace_count': workspace_count,
... 'user_profile': user_profile,
... })
>>> variables
[{'False': False, 'None': None, 'True': True}, {'workspace_count': '', 'locations_not_reporting': '', 'fresh_locations': '', 'inalarm_stores': '', 'locations_reporting': '', 'webalert_locations': '', 'user_profile': '', 'inalarm_count': ''}, {u'csrf_token': <django.utils.functional.__proxy__ object at 0x10439fcd0>, u'sql_queries': [], 'perms': <django.contrib.auth.context_processors.PermWrapper object at 0x10439fe50>, 'messages': [], u'request': <WSGIRequest
path:/www.google.com,
GET:<QueryDict: {}>,
POST:<QueryDict: {}>,
COOKIES:{},
META:{u'HTTP_COOKIE': u'',
u'PATH_INFO': u'www.google.com',
u'QUERY_STRING': '',
u'REMOTE_ADDR': '127.0.0.1',
u'REQUEST_METHOD': 'GET',
u'SCRIPT_NAME': u'',
u'SERVER_NAME': 'testserver',
u'SERVER_PORT': '80',
u'SERVER_PROTOCOL': 'HTTP/1.1',
u'wsgi.errors': <_io.BytesIO object at 0x1042fce30>,
u'wsgi.input': <django.test.client.FakePayload object at 0x104391550>,
u'wsgi.multiprocess': True,
u'wsgi.multithread': False,
u'wsgi.run_once': False,
u'wsgi.url_scheme': 'http',
u'wsgi.version': (1, 0)}>, 'api_header': {'content-type': 'application/json', 'Authorization': 'YmFrZXItYXBpOlVuaW9uMTIz'}, u'STATIC_URL': '/static/', u'LANGUAGES': (('en', 'English'),), 'api_address': 'http://69.164.69.214/BakerPublic/api', 'user': <django.contrib.auth.models.AnonymousUser object at 0x10439fe10>, u'LANGUAGE_CODE': u'en-us', u'debug': True, 'DEFAULT_MESSAGE_LEVELS': {'DEBUG': 10, 'INFO': 20, 'WARNING': 30, 'SUCCESS': 25, 'ERROR': 40}, u'LANGUAGE_BIDI': False, u'MEDIA_URL': '/media/'}]
>>> type(variables)
<class 'django.template.context.RequestContext'>
>>> dict(variables)
Traceback (most recent call last):
File "<console>", line 1, in <module>
ValueError: dictionary update sequence element #0 has length 15; 2 is required
来自django.template导入请求上下文的>>
>>>从django.test.client导入请求工厂
>>>请求工厂=请求工厂()
>>>request=request\u factory.get('www.google.com')
>>>新数据位置、webalert位置、报告位置、未报告位置、inalarm计数、inalarm存储、工作区计数、用户配置文件=“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”
>>>变量=请求上下文(请求{
…“新位置”:新数据位置,
…“webalert\u位置”:webalert\u位置,
…位置报告:报告位置,
…“地点不报告”:地点不报告,
…'inalarm_count':inalarm_count,
…inalarm商店:inalarm商店,
…工作区计数:工作区计数,
…“用户配置文件”:用户配置文件,
... })
>>>变数
[{'False':False,'None':None,'True':True},{'workspace_count':'','locations_not_reporting':'','fresh_locations':'','locations_reporting':'','webalert_locations':'','user_profile':'','','inalarm计数':'','u'csrf_标记','u'sql_查询':[],'perms','messages':[],'messages','messages':'',u'request请求','api"标头:{'content-type':'application/json','Authorization':'YmFrZXItYXBpOlVuaW9uMTIz',u'STATIC_URL':'/STATIC/',u'LANGUAGES':('en','English'),),'api_address':'http://69.164.69.214/BakerPublic/api','user':,u'LANGUAGE_CODE':u'en-us',u'debug':True,'DEFAULT_MESSAGE_LEVELS':{'debug':10,'INFO':20,'WARNING':30,'SUCCESS':25,'ERROR':40},u'LANGUAGE_BIDI':False,u'MEDIA_URL':'/MEDIA/'}]
>>>类型(变量)
>>>dict(变量)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
ValueError:字典更新序列元素#0的长度为15;需要2
因此,django 1.6 dir()函数返回时没有错误,但在1.7实现中它失败了
当使用django.views.generic中基于类的视图时,很多这方面的工作都是为您完成的。如果您查看TemplateView,您会发现它继承了TemplateResponseMixin的render_to_响应方法,TemplateResponseMixin使用TemplateResponse来完成所有这些工作。另外,请查看ContextMixin,它具有get_context_data方法。最干净的我认为将上下文数据传递到模板的方式。更多关于泛型/基于类的视图。我强烈怀疑变量
应该是一个普通的字典,而不是RequestContext
这里。谢谢。它对我使用的视图起到了作用。我"我从以前的程序员那里接手,直到更新到1.7,这一切都在运行。我回顾了1.6,render_to_响应中似乎没有任何变化。你知道为什么它以前会工作吗?@Brandon:我怀疑这是一个巧合,它曾经工作过;文档清楚地说明了一个字典是可以使用的好的。非常感谢。很简单,我错过了。非常感谢。