Python 在 jinja2 中转义引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17941109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:32:28  来源:igfitidea点击:

Escaping quotes in jinja2

pythonjinja2

提问by Razmig

I am building a json object in jinja file:

我正在 jinja 文件中构建一个 json 对象:

object_name = {
    property_name: "{{ _("Some Text which might have "quotes" in it")  }}"
}

And then import the above jinja2 file in a script tag

然后在脚本标签中导入上面的jinja2文件

note: _("Text") is used to be replaced by a translation text, so the text in the () will be replaced with text of another language so i can not predict if the translation will contain double quotes

注意:_("Text") 用于替换为翻译文本,因此 () 中的文本将替换为另一种语言的文本,因此我无法预测翻译是否包含双引号

any idea how to escape the incoming quotes and convert them to for example "

知道如何转义传入的引号并将它们转换为例如“

Edited

已编辑

The solution:

解决方案:

The solution to this problem for us was by making python go through all the translations and escape all qoutations. but we always have to make sure at least the english text not to be problematic and anyway we have controll over this.... so far :)

对我们来说,解决这个问题的方法是让 python 遍历所有翻译并逃避所有 qoutations。但我们总是必须确保至少英文文本没有问题,无论如何我们已经控制了这个......到目前为止:)

Look at this document aswell

看看这个文件

http://pology.nedohodnik.net/doc/user/en_US/ch-poformat.html#sec-poescapes

http://pology.nedohodnik.net/doc/user/en_US/ch-poformat.html#sec-poescapes

回答by Ajay

didn't understand the question clearly. if escaping with single backslashes didn't work, escape backslashes as well using

没看清楚问题。如果使用单个反斜杠转义不起作用,也可以使用反斜杠转义

object_name = {
    property_name: "{{ _(\\"Some Text which might have \\"quotes\\" in it\\")  }}"
}

回答by Garrett

In flask, there is a default filter called tojsonthat you could use or, with plain jinja2, you can create your own tojsonfilter:

在 中flask,有一个默认过滤器可供您tojson使用,或者使用 plain jinja2,您可以创建自己的tojson过滤器:

>>> import json
>>> env = jinja2.Environment()
>>> env.filters['tojson'] = json.dumps
>>> tmpl = env.from_string("""\
object_name = {
    property_name: {{ _(text)|tojson  }}
}""")
>>> print tmpl.render({'_': lambda x: x, 'text': 'Some text with "Quotes"'})
object_name = {
    property_name: "Some text with \"Quotes\""
}

回答by Alexander Chzhen

Jinja2 has nice filter tojson. If you make json from string, it will generate string enclosed in double quotes "". You can safely use it in javascript. And you don't need put quotes around by yourself.

Jinja2 有很好的过滤器tojson。如果从字符串生成 json,它将生成用双引号 "" 括起来的字符串。您可以安全地在 javascript 中使用它。而且你不需要自己加上引号。

string = {{ html_string|tojson }};

Another option - create dict in Python and then convert it to javascript object with single use of

另一种选择 - 在 Python 中创建 dict,然后将其转换为 javascript 对象,并一次性使用

jsObject = {{ py_dict|tojson }};