On Tue, Nov 17, 2009 at 1:12 PM, Tom Evans <tevans...@googlemail.com> wrote:

> This appears to be some sort of issue between the mysqldb and django's
> templating system.
>
> This code generates the UnicodeEncodeError as shown above, when the model
> is saved:
>
>
>       from django.template import loader
>       content = loader.render_to_string('the_template.html',
> context_instance=ctxt)
>       self.task.generated_content = content
>
> This code does not:
>
>
>       from django.template import loader
>       content = loader.render_to_string('the_template.html',
> context_instance=ctxt)
>       self.task.generated_content = content[:]
>
> IE, taking a copy of the string that was rendered avoids mysql barfing on
> it as input. Bonus points if someone can explain what on earth it is that
> django is doing to a unicode string that stops mysql treating it as a
> unicode string!
>
>
content above is not a simple unicode string:

>>> from ttt.models import Content
>>> from django.template import loader
>>> c = loader.render_to_string('uni.html')
>>> c
u'\u2019'
>>> type(c)
<class 'django.utils.safestring.SafeUnicode'>

Copying it the way you did turns it into a simple unicode type:

>>> c2 = c[:]
>>> type(c2)
<type 'unicode'>

However, even the SafeUnicode version works for me:

>>> m=Content(content=c)
>>> m.save()
>>>

Yes, I am using MySQL backend.  But I'm also using Django 1.1.1.  I believe
the problem you have found here was found and fixed for ticket #6052:

http://code.djangoproject.com/ticket/6052

This fix went into the 1.0.X branch about a year ago.  You really should run
the current 1.0.X release, not 1.0.   Branch releases contain bug fixes
only, so there should be no concern about backwards incompatibilities in
upgrading to the latest branch release.

Karen

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=.


Reply via email to