错误:“字典更新序列元素#0长度为1; 2是必需的“在Django 1.4上
我在django 1.4上有一个错误信息:
字典更新序列元素#0具有长度1; 2是必需的
[编辑]
当我尝试使用模板标签时发生这种情况:`{%for v in values%}:
dictionary update sequence element #0 has length 1; 2 is required Request Method: GET Request URL: ... Django Version: 1.4.5 Exception Type: ValueError Exception Value: dictionary update sequence element #0 has length 1; 2 is required Exception Location: /usr/local/lib/python2.7/dist-packages/djorm_hstore/fields.py in __init__, line 21 Python Executable: /usr/bin/uwsgi-core Python Version: 2.7.3 Python Path: ['/var/www/', '.', '', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/pymodules/python2.7'] Server time: sam, 13 Jul 2013 16:15:45 +0200 Error during template rendering In template /var/www/templates/app/index.html, error at line 172 dictionary update sequence element #0 has length 1; 2 is required 172 {% for product in products %} Traceback Switch to copy-and-paste view /usr/lib/python2.7/dist-packages/django/core/handlers/base.py in get_response response = callback(request, *callback_args, **callback_kwargs) ... ▶ Local vars /usr/lib/python2.7/dist-packages/django/contrib/auth/decorators.py in _wrapped_view return view_func(request, *args, **kwargs) ... ▶ Local vars /usr/lib/python2.7/dist-packages/django/views/decorators/http.py in inner return func(request, *args, **kwargs) ... ▶ Local vars ./app/views.py in index context_instance=RequestContext(request)) ... ▶ Local vars /usr/lib/python2.7/dist-packages/django/shortcuts/__init__.py in render_to_response return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs) ... ▶ Local vars /usr/lib/python2.7/dist-packages/django/template/loader.py in render_to_string return t.render(context_instance) ... ▶ Local vars /usr/lib/python2.7/dist-packages/django/template/base.py in render return self._render(context) ... ▶ Local vars /usr/lib/python2.7/dist-packages/django/template/base.py in _render return self.nodelist.render(context) ... ▶ Local vars /usr/lib/python2.7/dist-packages/django/template/base.py in render bit = self.render_node(node, context) ... ▶ Local vars /usr/lib/python2.7/dist-packages/django/template/debug.py in render_node return node.render(context) ... ▶ Local vars /usr/lib/python2.7/dist-packages/django/template/loader_tags.py in render return compiled_parent._render(context) ... ▶ Local vars /usr/lib/python2.7/dist-packages/django/template/base.py in _render return self.nodelist.render(context) ... ▶ Local vars /usr/lib/python2.7/dist-packages/django/template/base.py in render bit = self.render_node(node, context) ... ▶ Local vars /usr/lib/python2.7/dist-packages/django/template/debug.py in render_node return node.render(context) ... ▶ Local vars /usr/lib/python2.7/dist-packages/django/template/loader_tags.py in render result = block.nodelist.render(context) ... ▶ Local vars /usr/lib/python2.7/dist-packages/django/template/base.py in render bit = self.render_node(node, context) ... ▶ Local vars /usr/lib/python2.7/dist-packages/django/template/debug.py in render_node return node.render(context) ... ▶ Local vars /usr/lib/python2.7/dist-packages/django/template/defaulttags.py in render len_values = len(values) ... ▶ Local vars /usr/lib/python2.7/dist-packages/django/core/paginator.py in __len__ return len(self.object_list) ... ▶ Local vars /usr/lib/python2.7/dist-packages/django/db/models/query.py in __len__ self._result_cache = list(self.iterator()) ... ▶ Local vars /usr/lib/python2.7/dist-packages/django/db/models/query.py in iterator obj = model(*row[index_start:aggregate_start]) ... ▶ Local vars /usr/lib/python2.7/dist-packages/django/db/models/base.py in __init__ setattr(self, field.attname, val) ... ▶ Local vars /usr/local/lib/python2.7/dist-packages/djorm_hstore/fields.py in __set__ value = self.field._attribute_class(value, self.field, obj) ... ▶ Local vars /usr/local/lib/python2.7/dist-packages/djorm_hstore/fields.py in __init__ super(HStoreDictionary, self).__init__(value, **params) ... ▶ Local vars
当我尝试访问hstore查询集时,也发生这种情况:
[编辑]
Traceback (most recent call last): File "manage.py", line 14, in <module> execute_manager(settings) File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 459, in execute_manager utility.execute() File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 382, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 196, in run_from_argv self.execute(*args, **options.__dict__) File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 232, in execute output = self.handle(*args, **options) File "/home/name/workspace/project/app/data/commands/my_command.py", line 60, in handle item_id = tmp[0].id, File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 207, in __getitem__ return list(qs)[0] File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 87, in __len__ self._result_cache.extend(self._iter) File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 301, in iterator obj = model(*row[index_start:aggregate_start]) File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 300, in __init__ setattr(self, field.attname, val) File "/usr/local/lib/python2.7/dist-packages/djorm_hstore/fields.py", line 38, in __set__ value = self.field._attribute_class(value, self.field, obj) File "/usr/local/lib/python2.7/dist-packages/djorm_hstore/fields.py", line 21, in __init__ super(HStoreDictionary, self).__init__(value, **params) ValueError: dictionary update sequence element #0 has length 1; 2 is required
代码是:
tmp = Item.objects.where(HE("kv").contains({'key':value})) if tmp.count() > 0: item_id = tmp[0].id,
我只是尝试访问我不明白“更新序列”消息的值。 当我使用游标而不是hstore queryset函数的作品。 错误也来自模板渲染,我刚刚重新启动uwsgi
,一切正常,但错误后来回来
[编辑]
有人有一个想法?
刚碰到这个问题。 我不知道这是否是打你的代码是一样的,但对我来说,根本原因是因为我忘了把name=
放在url
函数调用的最后一个参数上。
例如,下面的问题将抛出错误:
url(r'^foo/(?P<bar>[A-Za-z]+)/$', FooBar.as_view(), 'foo')
但是这实际上起作用:
url(r'^foo/(?P<bar>[A-Za-z]+)/$', FooBar.as_view(), name='foo')
回溯是无用的原因是因为在内部,Django想要将给定的位置参数parsing为关键字参数kwargs
,并且由于string是可迭代的,所以非典型代码path开始展开。 始终在您的url上使用name=
!
当您尝试如下所示的内容时,会引发问题中的错误:
>>> a_dictionary = {} >>> a_dictionary.update([[1]]) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: dictionary update sequence element #0 has length 1; 2 is required
除非您显示代码,完整的回溯,否则很难说明代码中的原因。
这是再现的错误。
>>> d = {} >>> d.update([(1,)]) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: dictionary update sequence element #0 has length 1; 2 is required >>> >>> d {} >>> >>> d.update([(1, 2)]) >>> d {1: 2} >>> >>> d.update('hello_some_string') Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: dictionary update sequence element #0 has length 1; 2 is required >>>
如果你给出了序列,任何元素的长度是1,需要两个,那么我们会得到这种错误。 看到上面的代码。 我第一次给了元组的序列,它的长度是1,那么我们得到了错误,字典没有更新。 第二次,我给了里面元素与两个元素,字典得到更新。
我得到了同样的问题,并发现这是由于错误的参数。 在views.py
,我使用了:
return render(request, 'demo.html',{'items', items})
但是我发现这个问题: {'items', items}
。 更改为{'items': items}
解决了这个问题。
将name=
添加到url定义的最后一项。
抛出错误url(r'^home$', 'common.views.view1', 'home'),
正确的url(r'^home$', 'common.views.view1', name='home'),
当我忘记将关键字参数名称传递给url()函数时,我遇到了上述问题。
代码有错误
url(r"^testing/$", views.testing, "testing")
代码没有错误
url(r"^testing/$", views.testing, name="testing")
所以最后我以这种方式删除了上述错误。 你的情况可能会有所不同。 所以在urls.py中检查你的url模式。
您正在发送一个参数不正确; 它应该是一个dictionary object
:
-
错误:
func(a=r)
-
正确:
func(a={'x':y})
在我的例子中,我的一个视图中return render(self.request, 'es_connection_error.html', {'error':error});
返回了return render(self.request, 'es_connection_error.html', {'error':error});
在try / catch块中而不是返回context
在尝试使用错误types的参数调用update方法时遇到此问题。 预计的字典是:
{'foo': True}
通过的是:
{'foo': "True"}
确保你检查你传递的所有参数是预期的types。
错误应该是与参数。 请确认参数是一个字典对象 。 如果它只是一个参数列表/元组只使用一个*( *params
)而不是两个*( **params
)。 这将把列表/元组分解为适量的参数。
或者,如果参数是来自代码的其他部分作为JSON文件,请做json.loads(params)
,因为JSON对象有时performance为string,所以您需要使用从string加载的JSON负荷)。
super(HStoreDictionary, self).__init__(value, **params)
希望这可以帮助!