python - <a></a> 标签放在引号中时不会呈现

标签 python html django google-app-engine

我知道这是一个微不足道的错误,但我无法调试它。
当我将标签放在引号中并动态生成链接的字符串时,不会生成“链接”。相反,“<”、“>”按原样呈现。 (我正在尝试在 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>  

以下是输出:

Output on browser

如有任何帮助,我们将不胜感激。 (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/

相关文章:

python - 如何在 Python/Django 中将 MP4 转换为具有多种音频和多种视频质量的 HLS?

django - 使所有内联表单集表单可选

python - Django : makemigrations process does not end

Python OpenCV : Why does fillPoly() only draw grey polygons, 无论其颜色参数如何?

html - 为什么我的 HTML 5 本地存储无法工作?

javascript - 如何按类名在每个元素上使用函数?

javascript - 使用 jQuery 删除两个标签之间的文本

python - Z3py:将 Z3 公式转换为 picosat 使用的子句

python - 逐行读取和写入txt文件的问题

django - Gunicorn 启动时抛出 OSError Errno 1