Hack Jinja2 to encode from `utf-8` instead of `ascii`?

后端 未结 1 1992
我寻月下人不归
我寻月下人不归 2020-12-19 22:17

Jinja2 converts all template variables into unicode before processing. Can anybody find a place where does this happen?

The problem is that it assumes that strings a

相关标签:
1条回答
  • 2020-12-19 22:47

    Answer from Armin:

    Unfortunately that is impossible. Jinja uses the default string coercion on 2.x that Python provides for speed. There are no guaranteed calls to make something unicode. The only shitty choice you have is to reload sys and call sys.setdefaultencoding('utf-8') or something.

    UPDATE: Jinja2 2.8 contains some updates related to implicit string conversions. This gives me the idea that it is possible to go without sys.setdefaultencoding('utf-8') by overriding __add__ methods of the unicode type and make sure that it is type is used first while concatenating strings.

    https://github.com/mitsuhiko/jinja2/issues/511

    0 讨论(0)
提交回复
热议问题