asselinpaul
asselinpaul

Reputation: 386

Jinja2 encoding to render

I have a python string:

<p> <a href="http://blog.alexmaccaw.com/how-to-travel-around-the-world-for-a-year">How to travel around the world for a year • Alex MacCaw<span class="small">blog.alexmaccaw.com</span> </a></p>

of <type 'str'>

the problem is caused by the when I try to render it using Jinja.

renderedPage = template.render(starred = the_string_i_mentioned_higher_up)

And end up with this error:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 125: ordinal not in range(128)

I've tried all sort of encoding and decoding but I always get a similar error.

Any thoughts?

Upvotes: 0

Views: 1439

Answers (2)

Wooble
Wooble

Reputation: 89917

Use unicode, not str objects for text in Python 2. Especially for text with non-ASCII characters. str only works coincidentally, sometimes.

(You also need to specify your source encoding if you have text with non-ASCII in your source file):

# coding: utf-8
mystring = u'<p> <a href="http://blog.alexmaccaw.com/how-to-travel-around-the-world-for-a-year">How to travel around the world for a year • Alex MacCaw<span class="small">blog.alexmaccaw.com</span> </a></p>'

(It's probably also a Bad Idea to have HTML in your Python code instead of in a template or the database...)

Upvotes: 0

Andrew Kloos
Andrew Kloos

Reputation: 4578

Try using the htmml code:

&middot;

Hope this helps!

Upvotes: 1

Related Questions