【发布时间】:2019-07-09 17:00:21
【问题描述】:
我正在使用 Python 3.7 和 Django。我想从 Python 中的模板中获取一个字符串,并像这样进行适当的替换......
src = Template(filein.read())
# document data
relative_path = article.path.replace(settings.REDDIT_URL_PREFIX, "")
d = {'relative_path': relative_path, 'comment': comment}
# do the substitution
result = src.substitute(d)
但是,有一个问题。我的模板包含这个
["xpath=//a[@onclick='$(this).parent().submit()']", "xpath:attributes"],
美元符号通常用于替换,所以也许是这个原因,我上面的代码因错误而死...
ValueError: Invalid placeholder in string: line 248, col 31
有谁知道我如何修改上面的模板行,以便替换机制忽略该行上的美元符号?
【问题讨论】:
标签: python django python-3.x templates substitution