错误:“字典更新序列元素#0具有长度1; 2是必需的“在Django 1.4上 [英] Error: "dictionary update sequence element #0 has length 1; 2 is required" on Django 1.4

查看:5711
本文介绍了错误:“字典更新序列元素#0具有长度1; 2是必需的“在Django 1.4上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在django 1.4上有一个错误消息:


字典更新序列元素#0的长度为1; 2需要




使用模板标签,如:`{%for v in values%}:

 字典更新序列元素#0的长度为1; 2需要

请求方法:GET
请求URL:...
Django版本:1.4.5
异常类型:ValueError
异常值:

字典更新序列元素#0的长度为1;需要$ 2

异常位置:__init__中的/usr/local/lib/python2.7/dist-packages/djorm_hstore/fields.py第21行
Python可执行文件:/ usr / bin / uwsgi-core
Python版本:2.7.3
Python路径:

['/ 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-软件包/ PIL',
'/usr/lib/pymodules/python2.7']

服务器时间:sam,2013年7月13日16:15:45 +0200
错误在模板渲染中

在模板/var/www/templates/app/index.html中,第172行错误
字典更新序列元素#0的长度为1; 2需要

172 {%产品中的产品%}

追溯切换到复制和粘贴视图

/ usr / lib / python2.7 / dist-packages / django / core / handlers / base.py in get_response

response = callback(request,* callback_args,** callback_kwargs)

..
▶本地vars
/usr/lib/python2.7/dist-packages/django/contrib/auth/decorators.py在_wrapped_view

return view_func(request,* args,** kwargs)

...
▶本地vars
/usr/lib/python2.7/dist-packages/django/views/decorators/http.py在内部

返回func(请求,* args,** kwargs)

...
▶本地vars
./app/views。 py in index

context_instance = RequestContext(request))

...
▶本地vars
/usr/lib/python2.7/dist -packages / django / shortcuts / __ init__.py in render_to_response

return HttpResponse(loader.re nder_to_string(* args,** kwargs),** httpresponse_kwargs)

...
▶本地vars
/usr/lib/python2.7/dist-packages/django /template/loader.py in render_to_string

return t.render(context_instance)

...
▶本地vars
/ usr / lib / python2.7 / dist-packages / django / template / base.py在render

return self._render(context)

...
▶本地vars
/usr/lib/python2.7/dist-packages/django/template/base.py在_render

返回self.nodelist.render(上下文)

...
▶本地vars
/usr/lib/python2.7/dist-packages/django/template/base.py在render

bit = self.render_node (节点,上下文)

...
▶render_node中的本地vars
/usr/lib/python2.7/dist-packages/django/template/debug.py b
$ b返回node.render(上下文)

...
▶本地vars
/usr/lib/python2.7/dist-packa ges / django / template / loader_tags.py在render

返回compiled_pa​​rent._render(上下文)

...
▶本地vars
/ usr /lib/python2.7/dist-packages/django/template/base.py在_render

返回self.nodelist.render(上下文)

...
▶本地vars
/usr/lib/python2.7/dist-packages/django/template/base.py在render

bit = self.render_node(node,context)

...
▶render_node中的本地vars
/usr/lib/python2.7/dist-packages/django/template/debug.py

return node.render(context)

...
▶本地vars
/usr/lib/python2.7/dist-packages/django/template/loader_tags.py在渲染

result = block.nodelist.render(上下文)

...
▶本地vars
/usr/lib/python2.7 /dist-packages/django/template/base.py在render

bit = self.render_node(node,context)

...
▶render_node中的本地vars
/usr/lib/python2.7/dist-packages/django/template/debug.py

返回node.render(上下文)

...
▶本地vars
/usr/lib/python2.7/dist-packages/django/template/defaulttags.py在渲染

len_values = len(values)

...
▶本地vars
/usr/lib/python2.7/dist-packages/django /core/paginator.py __len__

return len(self.object_list)

...
▶本地vars
/ usr / lib / python2.7 / dist-packages / django / db / models / query.py in __len__

self._result_cache = list(self.iterator())

...
▶本地vars
/usr/lib/python2.7/dist-packages/django/db/models/query.py在迭代器

obj = model(* row [ index_start:aggregate_start])

...
▶本地vars
/usr/lib/python2.7/dist-packages/django/db/models/base.py __init__

setattr(self,field.attname,val)

...
▶本地vars
/usr/local/lib/python2.7/ dist-packages / djorm_hstore / fields.py __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)

...
▶本地vars

它当我尝试访问hstore查询器时也会发生:



 追溯(最近的最后一次呼叫):
文件manage.py,第14行在< module>
execute_manager(settings)

文件/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py,第459行,在execute_manager
utility.execute()

文件/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py,第382行,执行
self.fetch_command(子命令).run_from_argv(self.argv)

文件/usr/local/lib/python2.7/dist-packages/django/core/management/base.py ,第196行,在run_from_argv
self.execute(* args,** options .__ dict__)

文件/usr/local/lib/python2.7/dist-packages/django /core/management/base.py,第232行,执行
output = self.handle(* args,** options)

文件/ home / name / workspace / project /app/data/commands/my_command.py,第60行,句柄
item_id = tmp [0] .id,

文件/usr/local/lib/python2.7 /dist-packages/django/db/models/query.py,第207行,__getitem__
返回列表(qs)[0]

文件/ usr / local / lib / python2.7 / DIST - 对ackages / django / db / models / query.py,第87行,__len__
self._result_cache.extend(self._iter)

文件/ usr / local / lib / python2 .7 / dist-packages / django / db / models / query.py,第301行,迭代器
obj = model(* row [index_start:aggregate_start])

文件/ usr / local / lib / python2.7 / dist-packages / django / db / models / base.py,第300行,__init__
setattr(self,field.attname,val)

文件/usr/local/lib/python2.7/dist-packages/djorm_hstore/fields.py,第38行,__set__
value = self.field._attribute_class(value,self.field,obj )

文件/usr/local/lib/python2.7/dist-packages/djorm_hstore/fields.py,第21行,__init__
super(HStoreDictionary,self).__ init__ (value,** params)

ValueError:字典更新序列元素#0的长度为1; 2需要

代码是:

  tmp = Item.objects.where(HE(kv)。contains({'key':value}))

如果tmp.count() > 0:

item_id = tmp [0] .id,

尝试访问的值我不明白更新序列的消息。当我使用游标而不是hstore queryset时,该功能起作用。错误也出现在模板渲染上,我刚刚重新启动了 uwsgi ,一切都很好,但错误后来出现



有人有个想法?

解决方案

刚跑进入这个问题。我不知道这是否与你的代码是一样的,但对我而言,根本原因是因为我忘记在$ $的最后一个参数上放入 name = c $ c> url 函数调用。



例如,以下内容会从问题引发错误:

  url(r'^ foo /(?P< bar> [A-Za-z] +)/ $',FooBar.as_view ')

但这实际上是有用的:

  url(r'^ foo /(?P< bar> [A-Za-z] +)/ $',FooBar.as_view(),name ='foo')

追溯无效的原因是内部,Django想解析给定的位置参数作为关键字参数 kwargs ,并且由于字符串是可迭代的,因此非典型代码路径开始展开。始终在您的网址上使用 name =


I have a error message on django 1.4:

dictionary update sequence element #0 has length 1; 2 is required

[EDIT]

It happened when I tried when using template tag like: `{% 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

It happens too when I try to access on a hstore queryset:

[edit]

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

the code is:

tmp = Item.objects.where(HE("kv").contains({'key':value}))

if tmp.count() > 0:

    item_id = tmp[0].id,

I just try to access to the value I don't understand the "update sequence" message. When I use a cursor instead of hstore queryset the function works. The error come on template rendering too , I just restarted uwsgi and everything works well, but error come back later

[edit]

Has someone an idea?

解决方案

Just ran into this problem. I don't know if it's the same thing that hit your code, but for me the root cause was because I forgot to put name= on the last argument of the url function call.

For instance, the following throws the error from the question:

url(r'^foo/(?P<bar>[A-Za-z]+)/$', FooBar.as_view(), 'foo')

But this actually works:

url(r'^foo/(?P<bar>[A-Za-z]+)/$', FooBar.as_view(), name='foo')

The reason why the traceback is unhelpful is because internally, Django wants to parse the given positional argument as the keyword argument kwargs, and since a string is an iterable, an atypical code path begins to unfold. Always use name= on your urls!

这篇关于错误:“字典更新序列元素#0具有长度1; 2是必需的“在Django 1.4上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆