Python Jinja2编码错误
我已经为此奋斗了一整晚 我正在尝试使用从.md文件生成HTML文件,并将它们嵌入到其他一些HTML文件中 以下是有问题的片段:Python Jinja2编码错误,python,markdown,jinja2,Python,Markdown,Jinja2,我已经为此奋斗了一整晚 我正在尝试使用从.md文件生成HTML文件,并将它们嵌入到其他一些HTML文件中 以下是有问题的片段: md = markdown.Markdown(encoding="utf-8") input_file = codecs.open(f, mode="r", encoding="utf-8") # f is the name of the markdown file text = input_file.read() html = md.convert(text) # h
md = markdown.Markdown(encoding="utf-8")
input_file = codecs.open(f, mode="r", encoding="utf-8") # f is the name of the markdown file
text = input_file.read()
html = md.convert(text) # html generated from the markdown file
context = {
'css_url': url_for('static', filename = 'markdown.css'),
'contents': html
}
rendered_file = render_template('blog.html', **context)
output = open(splitext(f)[0] + '.html', 'w') # write the html to disk
output.write(rendered_file)
output.close()
这是我的“blog.html”模板,非常简单:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>blog</title>
<link rel="stylesheet" href="{{ css_url }}" type="text/css" />
</head>
<body>
{{ contents }}
</body>
</html>
博客
{{contents}}
但这就是我得到的:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>blog</title>
<link rel="stylesheet" href="/static/markdown.css" type="text/css" />
</head>
<body>
<li>People who love what they are doing</li>
<li></li>
</ol>
</body>
</html>
博客
li热爱自己所做工作的人/li
李/李
/ol
所以我得到了那些奇怪的“>”、“<”东西,尽管我已经将编码指定为“utf-8”。可能会出什么问题
谢谢大家!
与编码无关。这些是表示输入的HTML实体。你应该这样做,金贾不会自动逃脱它
{{ contents|safe }}
天哪,你刚刚救了我的命。。。非常感谢你!!