如何在Jinja2中渲染时转义双引号?

我使用Jinja 2和Python 3来创建Golang代码,需要将引号中的一些参数传递给最后代码中的一个函数,但是Jinja 2没有转义双引号。我的python代码类似于:

list_s = ['a', 'b']
string = '\"' + '", "'.join(list_s) + '\"'
final_string = 'Function(' + string + ')'
print(final_string)

template.render({'function': final_string})

我的模板是:

e.({{function}})

我在控制台中得到的内容(python代码中的print):

Function("a", "b")

我在最后的代码中想要的是:

e.(Function("a", "b"))

我在我的最后代码中实际得到的是:

e.(Function("a", "b"))

我已经试过了:

'`\"`' , '`"`', "'\"'", "\\\"", "\N{Quotation Mark}"

他们都不像我想的那样工作。有什么想法吗?

谢谢 :))

"已解决":

我的python代码从双引号改为了双引号,所以我的python代码现在是:

string = '`' + '`, `'.join(list_s) + '`'

我的最终Go代码是:

e.(Function(`a`, `b`))

这个就可以了。这不是最好的解决办法,但最起码有用......

The alternative way to do this would have been

e.({{ function|safe }})

which prevents auto-escaping.

This is due to Jinja2 autoescaping. As described in the documentation, the recommended way to avoid this is to wrap the text in a Markup object.