- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我在 Django 1.4 上有一条错误消息:
dictionary update sequence element #0 has length 1; 2 is required
当我尝试使用模板标签时发生这种情况:{% 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
的最后一个参数上(或 Django 2.0+ 中的 path
)函数调用。
例如,以下函数会从问题中抛出错误:
url(r'^foo/(?P<bar>[A-Za-z]+)/$', views.FooBar.as_view(), 'foo')
path('foo/{slug:bar}/', views.FooBar, 'foo')
但这些确实有效:
url(r'^foo/(?P<bar>[A-Za-z]+)/$', views.FooBar.as_view(), name='foo')
path('foo/{slug:bar}/', views.FooBar, name='foo')
traceback 无用的原因是因为在内部,Django 想要将给定的 positional 参数解析为 keyword 参数 kwargs
,并且由于字符串是可迭代的,因此开始展开非典型代码路径。始终在您的网址上使用 name=
!
关于python - 错误 : "dictionary update sequence element #0 has length 1; 2 is required" on Django 1. 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17610732/
我正在用 Python (2.6) 编写一个应用程序,需要我使用字典作为数据存储。 我很好奇拥有一个大字典是否更节省内存,或者将其分解为许多(很多)较小的字典,然后拥有一个包含对所有较小字典的引用的“
Convert this [ "Cat" : ["A" : 1, "B": 2], "Mat" : ["C" : 3, "D": 4] ] Into [ "A" : 1,
有什么很酷的快速方法可以让两个字典创建第三个字典,以内连接方式将第一个字典的键映射到第二个字典的值? Dictionary dic1 = new Dictionary {{a1,b1},{a2,b2}
我希望将字典相互嵌套,以便容纳 block 的 xy 坐标。所以我会 IDictionary, IDictionary> 键 Dictionary 包含列、行组合,而值 Dictionary 包含 x
在 C# 中,我需要将数据保存在字典对象中,如下所示: Dictionary> MyDict = new Dictionary>(); 现在我意识到,在某些情况下我需要一些其他(不是字典类的)
第一个Dictionary就像 Dictionary ParentDict = new Dictionary(); ParentDict.Add("A_1", "1")
我似乎无法理解这个问题。我需要使用 LINQ 按内部字典的值对字典进行排序。有什么想法吗? 最佳答案 你的意思是你想要所有的值,按内部值排序? from outerPair in outer from
我想建模一个模式,其中响应是字典: { 'id1': { 'type': 'type1', 'active': true, }, 'id2': { 'type':
我有以下代码要添加或更新(如果已经存在)dict()-dict 中的值: if id not in self.steps: self.steps[ id ] = step else:
我有一个包含字典的 Swift 字典,我想使用存储的属性来访问键值: var json = [NSObject:AnyObject]() var title: String { get
我想创建一个 Dictionary>结构,我想提供一个 IEqualityComparer在包含 APerson 的second 字典中作为关键 如果我只有内部字典,那就是 var f = new D
我有一个集合,其中包含如下文档:文档 1: { "company": "ABC" "application": { "app-1": {"earning_from_src_A": 50,
我正在快速学习。 我发现 dictionary 就像 hash 用于 PHP 或其他一些语言。 那我怎么制作dictionary的dictionary呢?? 我有这样的数据 key:J name:jh
这个问题在这里已经有了答案: Explode a dict - Get all combinations of the values in a dictionary (2 个答案) 关闭 5 个月前
我是编程新手,所以如果我的问题看起来很愚蠢,我很抱歉。我想问一下有没有办法从 Multi.Dictionary 返回key当我有值(value)? 这是我的代码: Dim myDict Set myD
我试图找出标准 Ada 库是否配备了“字典”类型(我的意思是:一种以 格式存储值的数据结构,我可以从中检索 value 使用相应的唯一 key)。 这样的数据结构存在吗?如果是这样,有人可以提供一个
我究竟做错了什么?根据我的测试,objDic.exists 永远不会给出 False! dim objDic set objDic = createobject("scripting.
我想创建一个复合类型,其中包含一个字典作为其命名字段之一。但是明显的语法不起作用。我敢肯定有一些我不明白的基本原理。下面是一个例子: type myType x::Dict() end Jul
julia> hotcell2vocab = Dict([(cell, i-1+vocab_start) for (i,cell) in enumerate(h
我有一个简单的问题:我对 Dictionary.Value 集合进行了很多次迭代,这让我很烦,我必须调用 .ToList() 然后才能调用 .ForEach(),因为它似乎没有可枚举的Dictiona
我是一名优秀的程序员,十分优秀!