Problem following the tutorial when using non English characters
|Reported by:||Owned by:||Jacob|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
I'm following http://www.djangoproject.com/documentation/tutorial1/ and its second part. Everything goes OK. Just, to try things, I modify most of the examples of code. So when I modified the polls/models.py to create the Poll object, instead of using pub_date's tutorial provided description, I wrote:
pub_date = models.DateTimeField('Fecha de publicación')
To make the script a valid python script, I added at the top:
# -*- coding: latin1 -*-
So later, much much later, when I'm adding the administrative interface, I reach the point where I have added the "class Admin: pass" to Poll and web browsed my way to see the first poll. Here, the poll is displayed, and the date description shows a broken character (the accented o): "Fecha de publicaci�n:" (not sure if that will get through, it shows a question mark inside a rotated square).
Well, I guess maybe Django wants unicode characters or stuff. So I modified my Poll class to read (note the unicode string):
pub_date = models.DateTimeField(u'Fecha de publicación')
Reloading the admin page I get:
UnicodeDecodeError at /admin/polls/poll/1/
'ascii' codec can't decode byte 0xc3 in position 75: ordinal not in range(128)
...along with a hell long of a callstack and environment dump. Since I'm used to these kind of things, I tried the following change:
pub_date = models.DateTimeField(u'Fecha de publicación'.encode("utf8"))
...which worked and didn't crash, but strikes me as unusually cumbersome to do. AFAICS the model doesn't properly treat unicode strings and the reason I get this OK with the utf8 encoding is just because that's what the output page charset is, and the result is processed by the browser. I guess if I switched that to latin1 now it would break visually as well, right? Is Django unable to handle internationalised stuff in the model?
If this is the case, I would add a note on the kind of strings you can use for the model.
Change History (5)
comment:3 Changed 10 years ago by
|Component:||Documentation → Internationalization|
|Status:||closed → reopened|