我知道这是一个微不足道的错误,但我无法调试它。
当我将标签放在引号中并动态生成链接的字符串时,不会生成“链接”。相反,“<”、“>”按原样呈现。 (我正在尝试在 GAE 中运行一个使用 django 模板框架的 python 脚本)
以下是我的代码:
from google.appengine.ext.webapp import template
...
html = html + template.render('templates/footer.html',
{'links': 'Enter <a href="/">another sighting</a>.'})
以下是模板中的“footer.html”:
<p>
{{ links }}
</p>
</body>
</html>
以下是输出:
如有任何帮助,我们将不胜感激。 (Firefox和Google Chrome都是这种情况)
最佳答案
将safe
过滤器添加到您的变量中:
{{ links|safe }}
或者使用 mark_safe
在 python 代码中将您的字符串标记为安全功能:
from django.utils.safestring import mark_safe
html = html + template.render('templates/footer.html',
{'links': mark_safe('Enter <a href="/">another sighting</a>.')})
这是 django 的 automatic HTML escaping机制。您可以通过 autoescape 来控制它模板标签,但我不建议这样做:-)
关于python - <a></a> 标签放在引号中时不会呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28006808/