我的模板中有这个:
<h2>
Users ({{ users|length }})
</h2>
我还有一个显示用户行的循环:
{% for user in users %}
<tr>
<td>
{{ user.email }}
etc...
</td>
</tr>
{% endfor %}
我看不出我做错了什么。用户表工作得很好。但是当我用长度过滤器添加h2时,我收到了这个错误:
Traceback (most recent call last):
File "/Users/sean/Projects/appcash.me/_git/www/bottle.py", line 856, in _handle
return route.call(**args)
File "/Users/sean/Projects/appcash.me/_git/www/bottle.py", line 1721, in wrapper
rv = callback(*a, **ka)
File "/Users/sean/Projects/appcash.me/_git/www/views/admin.py", line 27, in custom_admin_page
return template('admin/users.html', users=users)
File "/Users/sean/Projects/appcash.me/_git/www/bottle.py", line 3487, in template
return TEMPLATES[tplid].render(kwargs)
File "/Users/sean/Projects/appcash.me/_git/www/datafly/core.py", line 184, in render
return self.tpl.render(**_defaults)
File "/Users/sean/Projects/appcash.me/_git/venv/lib/python2.7/site-packages/jinja2/environment.py", line 969, in render
return self.environment.handle_exception(exc_info, True)
File "/Users/sean/Projects/appcash.me/_git/venv/lib/python2.7/site-packages/jinja2/environment.py", line 742, in handle_exception
reraise(exc_type, exc_value, tb)
File "<template>", line 3, in top-level template code
File "<template>", line 57, in top-level template code
File "<template>", line 8, in block "content"
TypeError: object of type 'Cursor' has no len()
答案 0 :(得分:4)
你可以试试这个:
{{ users.count() }}
答案 1 :(得分:0)
您的users
变量似乎是Cursor
类型(没有长度)。
也许你打算在你的python(而不是模板)代码中做rows = users.fetchall()
?然后len(rows)
将起作用,模板中也会rows|length
。