Skip to content

UnicodeDecodeError Exception Fix On Templates

by Paul Kenjora on May 14th, 2008

My friend Brian at HelpMeSue.com has found a work around for the dreaded UnicodeDecodeError exception. Here is his fix…

In Brian’s Words…

Using special Spanish characters. Try to load a template with a render_to_response and get a template UnicodeDecodeError exception thrown.

To fix the issue:

  1. Run file on the template:

    file static_page.htm

    That produces: static_page.htm: ISO-8859 text, with very long lines, with CRLF line terminators
  2. Convert the page to utf-8:

    iconv -f ISO-8859-1 -t UTF-8 static_page.htm > new_static.htm

    cp new_static_htm static_page.htm

    file static_page.htm

    That produces: static_page.htm: UTF-8 Unicode text, with very long lines, with CRLF line terminators

And now it all work so Brian’s Spanish template guru is Help Me Sue compatible.

  • It was one of those cases where I outsourced the development for a static page. My static pages all inherit from my base.html, which does do some db queries. The page was emailed to me, I saved it, and boom...it didn't work. I searched around for the answer and found that many people had this problem, but all I found was about using __unicode__ instead of __str__ for db stuff. This was not the problem I had. The solution to my issues was solved by what I described above.
  • I'm also not sure what problem did you have.
    For me seems that your editor (that you created template with) uses iso-8859-1 instead of utf-8.
    Or maybe you had just a problem how to determine encoding and how to convert it to utf-8?
  • In this case I have a template with Spanish characters. Nothing was coming out of the database with Spanish chars.
  • This was a render_to_response with Spanish chars in the template. Nothing coming out of the database.
  • I don't understand this problem. You shouldn't have to go through all that work just to render a template with Spanish characters. Are the Spanish Characters hard-coded into the template or coming from a database? I'm really curious why would be getting this exception and Django couldn't handle it alone.
  • wow, this is another problem i'm trying to deal with!
    thanks
blog comments powered by Disqus