python - 在 web.py 中呈现 HTML

标签 python html templates web.py

我正在处理一个类似维基百科的项目。我可以使用 Markdown 将文本文件转换为 html 代码。我的问题是,我想在 html 文件中呈现此 html 代码。这是我的代码,

class articles:
    def GET(self):
        form_name=web.input()
        article_name=form_name.page
        article_file_path=os.path.join('articles',article_name)
        fp = open(article_file_path,'rU')
        text = fp.read()
        body = markdown2.markdown(text)
        return render.article_files(article_name, body)

我将 article_name 和 body(html 代码)传递给 article_files.html。 body 看起来像,

<h1>Hai</h1>
<p>Welcome<em>Ajay</em></p>

问题是,正文按原样显示。即 html 代码与所有标签一起打印在屏幕上。我想呈现此 html 代码(正文),如


欢迎 Ajay

我的 HTML 文件是:

$def with(title,content)
<html>
<head>
<title>$title</title>
</head>
<body>
    <form name="form" method="GET">
        $content
    </form>
</body>
</html>

最佳答案

HTML 转义在 web.py 模板中默认开启。要关闭它,请在变量名前加上一个冒号:

<form name="form" method="GET">
    $:content
</form>

确保潜在的恶意用户无法将任意 HTML 提供给您未转义的模板。

关于python - 在 web.py 中呈现 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6979819/

相关文章:

python - NLTK 词性缩写引用

c++ - PyTuple_Pack 接受可变数量的参数

python - 如何使用selenium python抓取多级下拉列表[依赖选择]

python - 如何使用 django-filters 重命名(在 API 中公开)过滤器字段名称?

javascript - 如何将 "ons-select"的值保存到 JavaScript 的变量中?

javascript - 如何使用 HTML 和 JS 在本地保存图像?

php - 调用 php shell_exec 命令的 html 按钮

c++ - 状态机实现中type_index的使用、销毁、同步

c++ - 前向声明的类型和 "non-class type as already been declared as a class type"

python - 在 django 模板中呈现 HTML(不是 unicode 而是 ASCII)