如何将对象实例传递给 jinja html 模板

sup*_*kar 5 python jinja2

我的班级看起来像这样

class Foo():
    x = attrib()
    y = attrib()


f = Foo(x=1, y=2)
Run Code Online (Sandbox Code Playgroud)

我的模板看起来像

<html>
<body>
 <span>{{x}}</span> -> instead of this I want to use foo.x
 <span>{{y}}</span> -> instead of this I want to use foo.y
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我用来template.render(**attr.asdict(f))渲染它。相反,我只想通过template.render(f)并使用{{f.x}}{{f.y}}

有没有办法做到这一点?我不断遇到错误

vars = dict(*args, **kwargs)
TypeError: 'Foo' object is not iterable
Run Code Online (Sandbox Code Playgroud)

sup*_*kar 0

这是通过使用解决的

Template.render(dict(foo=f))
Run Code Online (Sandbox Code Playgroud)