Q:

Jinja2 weird encoding error

I have been fighting this for a whole night...
I'm trying to use Python markdown to generate HTML files from .md files and embed them into some other HTML files.
Here is the problematic snippet:
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()

Here is my "blog.html" template , which is really simple:
<!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>

And yet this is what I get:
<!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>
&lt;li&gt;People who love what they are doing&lt;/li&gt;
&lt;li&gt;&lt;/li&gt;
&lt;/ol&gt;
</body>

</html>

So I'm getting those weird "&gt", "&lt" stuff, even though I've already specified the encoding to be 'utf-8'.  What could possibly go wrong?
Thank you!

A:

&lt;&gt; has nothing to do with encoding. These are HTML entities that represent your input. You should mark it as safe so that jinja will not automatically escape it.
{{ contents|safe }}

