【问题标题】:Python django escape unicodePython django 转义 unicode
【发布时间】:2011-06-28 20:12:54
【问题描述】:

我想转义 unicode。

我的 python 脚本包含:

unicode_str = u'\u8fb0'

我的模板文件包含:

{{ unicode_str|escape }}

这会导致 KeyError。

如何转义 unicode 字符?

谢谢!

【问题讨论】:

  • 你有完整的回溯吗?

标签: python django unicode django-templates


【解决方案1】:

在您的 python 代码中,您可以在传递整个对象时使用以下代码来转义 unicode:

import json
dict = {'name': 'John Doe'}
json.dumps(dict)

【讨论】:

    【解决方案2】:

    内置的转义过滤器不是为处理 Unicode 而设计的。自己写。

    【讨论】:

    • 你能给我指出一个至少给我一个例子的网站吗?
    • 我想我会从django doc开始
    猜你喜欢
    • 1970-01-01
    • 2019-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-29
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    相关资源
    最近更新 更多