Adrian Holovaty wrote: > On 11/30/06, gabor <[EMAIL PROTECTED]> wrote: >> but if i want to render it in a template and it contains non-ascii text, >> it fails in /home/gabor/src/django/django/template/__init__.py, >> line 745, UnicodeEncodeError. >> >> the code there calls str() on the form, and it's a problem because it >> calls Form.__str__, which in turn calls as_table, which returns unicode >> data. >> which is then converted to bytestring, by the default-charset (ascii), >> and that fails. > > Thanks for bringing this up. A clean way to solve the problem would be > to change the template system to convert any Unicode objects to > bytestring according to DEFAULT_CHARSET. What do you think? >
i generally agree, but the problem is.. this is the function in question: /home/gabor/src/django/django/template/__init__.py (around line 745) > def encode_output(self, output): > # Check type so that we don't run str() on a Unicode object > if not isinstance(output, basestring): > return str(output) > elif isinstance(output, unicode): > return output.encode(settings.DEFAULT_CHARSET) > else: > return output as you see, it already checks for unicode-strings. the problem is, that Form is not an unicode-string, so the code simply arrives in the "return str(output)" branch. what about the following change?: > if not isinstance(output, basestring): > - return str(output) > + try: > + return str(output) > + except UnicodeEncodeError: > + return unicode(output).encode(settings.DEFAULT_CHARSET) > elif isinstance(output, unicode): > return output.encode(settings.DEFAULT_CHARSET) from the python docs (http://docs.python.org/lib/built-in-funcs.html) unicode() technically should not work in this case, but it somehow works :) this change would not introduce any backward incompatibility, because this new code is only triggered when the old one failed. another (independent from this change) enhancement would be to add an __unicode__ method to the Form object. gabor --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-developers?hl=en -~----------~----~----~----~------~----~------~--~---
