fixed render() 必须用字典调用,而不是上下文,但似乎无法在模板中渲染变量,为什么?姜戈

fixed render() must be called with a dict, not a Context but can't seem to render the variable in template why? django

我在 django 中使用内置的电子邮件系统,但现在我收到这样的警告..

RemovedInDjango110Warning: render() must be called with a dict, not a Context

我的代码曾经是get_template(html_template).render(Context(body['ctx']))

我已经通过这样做修复了它

get_template(html_template).render({Context(body['ctx'])})

但在我修复它之后,不知何故我无法再调用我的电子邮件模板中所需的变量。

body['ctx']实际上包含诸如

之类的内容
{'type': 'type1', 'field': 'field1}

之前我还在用warning的方式发邮件的时候,在email模板中只能用{{type}}或者{{field}}但是现在修复了warning之后,就不能调用了变量了。我尝试将代码更改为 get_template(html_template).render({'content': Context(body['ctx'])}) 这仍然没有用,在电子邮件模板中然后我尝试 {{content}} 然后我会得到这样的东西 [{'False': False, 'None': None, 'True': True}, {'type': 'type1', 'field': 'field1'}]

所以我想我可以通过 {{ content.1.type }} 调用类型变量,但仍然一无所获。

谁能告诉我我可能做错了什么或者我还可以在这里尝试什么?

提前致谢

因为body['ctx']已经是一个字典,你只需要在render()函数中传递它即可。

get_template(html_template).render(body['ctx'])