gpt4 book ai didi

python - 如何正确使用 Jinja2 计数/长度而不会出错?

转载 作者:行者123 更新时间:2023-11-28 21:55:27 29 4
gpt4 key购买 nike

我的模板中有这个:

<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()

最佳答案

你可以试试这个:

{{ users.count() }}

关于python - 如何正确使用 Jinja2 计数/长度而不会出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22724243/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com