UnicodeDecodeError

192 views
Skip to first unread message

Hassan

unread,
Jan 5, 2012, 6:18:52 AM1/5/12
to Django users
Dear ALL,

i am trying to do render_to_response('index.html') , and i keep geting
this dont know why ? its a normal html page ??!! what to do ?


UnicodeDecodeError at /
'utf8' codec can't decode byte 0x93 in position 3197: invalid start
byte
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.3.1
Exception Type: UnicodeDecodeError
Exception Value:
'utf8' codec can't decode byte 0x93 in position 3197: invalid start
byte
Exception Location: c:\python27\lib\encodings\utf_8.py in decode, line
16
Python Executable: c:\python27\python.exe
Python Version: 2.7.2
Python Path:
['C:\\global',
'c:\\python27\\lib\\site-packages\\setuptools-0.6c11-py2.7.egg',
'c:\\python27\\lib\\site-packages\\django_blog_zinnia-0.8.1-
py2.7.egg',
'c:\\python27\\lib\\site-packages\\beautifulsoup-3.2.0-py2.7.egg',
'c:\\python27\\lib\\site-packages\\django_xmlrpc-0.1.3-py2.7.egg',
'c:\\python27\\lib\\site-packages\\django_tagging-0.3.1-py2.7.egg',
'c:\\python27\\lib\\site-packages\\django_mptt-0.5.2-py2.7.egg',
'c:\\python27\\lib\\site-packages\\pyparsing-1.5.6-py2.7.egg',
'c:\\python27\\lib\\site-packages\\akismet-0.2.0-py2.7.egg',
'c:\\python27\\lib\\site-packages\\south-0.7.3-py2.7.egg',
'c:\\python27\\lib\\site-packages\\django_oembed-0.1.1-py2.7.egg',
'C:\\Windows\\system32\\python27.zip',
'c:\\python27\\DLLs',
'c:\\python27\\lib',
'c:\\python27\\lib\\plat-win',
'c:\\python27\\lib\\lib-tk',
'c:\\python27',
'c:\\python27\\lib\\site-packages',
'c:\\python27\\lib\\site-packages\\PIL',
'c:\\python27\\lib\\site-packages\\win32',
'c:\\python27\\lib\\site-packages\\win32\\lib',
'c:\\python27\\lib\\site-packages\\Pythonwin']
Server time: Thu, 5 Jan 2012 06:11:04 +0200

Piotr Zalewa

unread,
Jan 5, 2012, 8:12:52 AM1/5/12
to django...@googlegroups.com
Please attach the index.html - there might be an unknown unicode character

zalun

Daniel Roseman

unread,
Jan 5, 2012, 8:16:57 AM1/5/12
to django...@googlegroups.com
On Thursday, 5 January 2012 11:18:52 UTC, Hassan wrote:
Dear ALL,

i am trying to do render_to_response('index.html') , and i keep geting
this dont know why ? its a normal html page ??!! what to do ?
 

You need to post some code.

However 99 times out of 100, when you see this error it's because the __unicode__ method in one of your models is not actually outputting unicode, and/or is converting wrongly. For instance, instead of this:

    def __unicode__(self):
        return 'foo %s' % self.bar

you should do this:

    def __unicode__(self):
        return u'foo %s' % self.bar

(note extra u).
--
DR.

César Frias

unread,
Jan 5, 2012, 8:16:39 AM1/5/12
to django...@googlegroups.com
I guess you have some files without the # -*- coding: utf-8 -*- on the top of the file

You will need this if you want to use some letters like é or ç...




Att,
César Frias
Linux user: #506849
Twitter: twitter.com/cesarFrias


2012/1/5 Piotr Zalewa <zal...@gmail.com>
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Karen Tracey

unread,
Jan 5, 2012, 8:27:52 AM1/5/12
to django...@googlegroups.com
On Thu, Jan 5, 2012 at 8:16 AM, César Frias <cagf...@gmail.com> wrote:
I guess you have some files without the # -*- coding: utf-8 -*- on the top of the file

You will need this if you want to use some letters like é or ç...


No, you get a different exception if you are missing the encoding declaration in a Python file that contains non-ASCII characters. In that case you get a SyntaxError that complains specifically about the missing encoding declaration.

Getting an error from the utf8 codec attempting to transform a bytestring to unicode implies you've got some bytestring data that is assumed to be encoded in utf8 but is in fact using some other encoding. Since this is happening when rendering a template, my guess would be the template file's encoding is something other than utf8. Django has a setting (https://docs.djangoproject.com/en/1.3/ref/settings/#file-charset) that controls what encoding is assumed for files read from disk, it defaults to utf8. Either fix the encoding of the template file to be utf8 or change that setting to match whatever encoding is being used for all your template files on disk.

Karen

francescortiz

unread,
Jan 5, 2012, 9:11:48 AM1/5/12
to Django users
Also, don't forget about the magic functions provided by django:
smart_unicode and smart_str

On Jan 5, 2:27 pm, Karen Tracey <kmtra...@gmail.com> wrote:

Hassan

unread,
Jan 5, 2012, 9:19:53 AM1/5/12
to Django users
i will post my index page now

Hassan

unread,
Jan 5, 2012, 9:20:49 AM1/5/12
to Django users

<html>
<head>


<title></title>

</head>
<body style="overflow:scroll;">
<table style="width:100%">
<tr>

<!-- Margin left -->
<td style="width:20%"></td>
<!-- end of Margin left -->

<!--Content-->
<td style="background-color:#1B4D90;">
<table>
<!--Menu-->
<tr><td>

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
width="800" height="200" id="movie_name" align="middle">
<param name="movie" value="static/menu.swf"/>
<!--[if !IE]>-->
<object type="application/x-shockwave-flash" data="static/
menu.swf" width="800" height="200">
<param name="movie" value="movie_name.swf"/>
<!--<![endif]-->
<a href="http://www.adobe.com/go/getflash">
<img src="http://www.adobe.com/images/shared/
download_buttons/get_flash_player.gif" alt="Get Adobe Flash player"/>
</a>
<!--[if !IE]>-->
</object>
<!--<![endif]-->
</object>
</td></tr>
<!--End of Menu-->

<!--Main Content-->
<tr><td><br/><center><img src="static/images/mainimage.jpg"
width="550px"></img></center></td></tr>
<!--End of Main Content-->

<!--Readmore and questions-->
<tr><td style="background-color:#CCCCCC">
<div style="background-color:#64B55B;width:100%">
<table style="width:100%">
<tr>
<td>
<p style="color:#ffffff;margin:15px">
<b style="color:#1B4D90;font-size:20px">About Us</b><br/>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In ut dictum
enim. Quisque posuere justo sem, posuere lobortis ligulaLorem ipsum
dolor sit amet, consectetur adipiscing elit. In ut dictum enim.
Quisque posuere justo sem, posuere lobortis ligula
</p>
</td>

<td>
<p style="color:#ffffff;margin:15px">
<b style="color:#1B4D90;font-size:20px">About Us</b><br/>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In ut dictum
enim. Quisque posuere justo sem, posuere lobortis ligulaLorem ipsum
dolor sit amet, consectetur adipiscing elit. In ut dictum enim.
Quisque posuere justo sem, posuere lobortis ligula
</p>
</td>
<td>
<p style="color:#ffffff;margin:15px">
<b style="color:#1B4D90;font-size:20px">About Us</b><br/>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In ut dictum
enim. Quisque posuere justo sem, posuere lobortis ligulaLorem ipsum
dolor sit amet, consectetur adipiscing elit. In ut dictum enim.
Quisque posuere justo sem, posuere lobortis ligula
</p>
</td>
</tr>
</table>
</div>
<br/><br/>
<table>
<tr>
<td style="width:60%;border-right:1px solid #073E75 ">

<h2 style="color:#073E75;margin-left:20px">Welcome</h2>
<p style="color:#073E75;text-indent:0px;margin-left:20px">Lorem ipsum
dolor sit amet, consectetur adipiscing elit. In ut dictum enim.
Quisque posuere justo sem, posuere lobortis ligula. Etiam erat diam,
elementum in dapibus bibendum, ultrices eget enim. Nulla et nibh nec
elit venenatis commodo sit amet pretium eros. Duis vel velit at augue
tristique vehicula. Nulla in lectus quis nisi egestas egestas at ac
lacus. Donec rutrum volutpat rhoncus. Proin condimentum, odio eu
commodo laoreet, turpis leo tincidunt nisi, nec mollis quam justo
vitae velit.</p>
<a href="" style="display: block;background-
color:#00A99D;color:#ffffff;width:75px;text-align:
center;float:right;margin-right:15%;margin-bottom:10px;Font-Family:
“Times New Roman”, Times, serif;text-decoration:none"><b><i>Read more</
i></b></a>
</td>
<td style="width:40%"></td>
</tr>
</table>
</td></tr>
<!--End of readmore and qestions-->

<!--foteer-->
<tr><td>
<p style="margin:10px;color:#cccccc">Global (c) 2011 | Privet policy
<br/> Powerd by <a href="http://www.gardeniatelco.com">Gardeniatelco</
a></p>
</td></tr>
<!--End of foteer-->
</table>
</td>
<!--end of Content-->
<!-- Margin Right -->
<td style="width:20%"></td>
<!-- end of Margin Right -->
</tr>
</table>
</body>
</html>

Simon Holness

unread,
Jan 5, 2012, 9:34:50 AM1/5/12
to django...@googlegroups.com
I would hazard a guess that your index.html file is actually not ascii
or utf-8 but some other encoding - probably cp1252

0x93 in cp1252 is a "smart quote" ( see
http://en.wikipedia.org/wiki/Windows-1252 ) so that would look like it
fits with the line quoted below.

Open it in an editor that knows about encodings and save it as utf-8
and all should hopefully be well.


2012/1/5 Hassan <haln...@gardeniatelco.com>:

J. Cliff Dyer

unread,
Jan 5, 2012, 10:51:11 AM1/5/12
to django...@googlegroups.com
Your problem is the curly quotes around “Times New Roman”. First, they
will prevent CSS from seeing the font as Times New Roman, and second,
they are encoded with the byte 0x93, which is how they show up in the
various Windows code pages (cp1252, for instance). Convert your file to
UTF-8, if you can.

Otherwise, remove all non-ascii characters from it. If you do this, but
you need non-ascii characters to show up in your web pages, you'll need
to use HTML escape sequences, like &#x201c;

Cheers,
Cliff

Hassan

unread,
Jan 5, 2012, 11:43:55 AM1/5/12
to Django users
thnx mate its working now ;)
Reply all
Reply to author
Forward
0 new messages