admin管理员组文章数量:1332889
My template is used for JS
let SETTINGS = {{settings|tojson(4)}};
My settings is a dict {'name': 'Russian name Саша', 'id': 12345}
. If I render it, i get:
let SETTINGS = {
"name": "Russian name \u0421\u0430\u0448\u0430",
"id": 12345
}
I need to get non-escaped unicode characters. The same way I can do in python using
json.dumps(data, encure_ascii=False)
But tojson()
filter accepts only one parameter (indent).
My template is used for JS
let SETTINGS = {{settings|tojson(4)}};
My settings is a dict {'name': 'Russian name Саша', 'id': 12345}
. If I render it, i get:
let SETTINGS = {
"name": "Russian name \u0421\u0430\u0448\u0430",
"id": 12345
}
I need to get non-escaped unicode characters. The same way I can do in python using
json.dumps(data, encure_ascii=False)
But tojson()
filter accepts only one parameter (indent).
1 Answer
Reset to default 10There is a way to supply rest parameters to tojson()
filter.
Starting with Jinja 2.9 policies can be configured on the environment which influence how filters and other template constructs behave. It can be done by changing some keys of policies attribute.
For your case there is json.dumps_kwargs
policy. This is what you have to do:
env = jinja2.Environment()
env.policies['json.dumps_kwargs']['ensure_ascii'] = False
This modified environment will not escape unicode symbols.
版权声明:本文标题:javascript - How to make tojson() filter in Jinja2 output Unicode instead of escape sequences? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742317377a2452091.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论