[Twisted-web] Rendering unicode instances.

Donovan Preston twisted-web@twistedmatrix.com
Wed, 28 Jan 2004 10:38:04 -0500


On Jan 28, 2004, at 5:11 AM, Syver Enstad wrote:

>
> What does one do to render Unicode objects with twisted.web (I am
> using woven also). I get UnicodeError's if I try to pass a unicode
> object that contains characters not in ASCII today.
>
> It would be nice if it was as simple as calling a method on the
> request setting output encoding which set the encoding in the HTTP
> headers and wrapped the output stream with an encoder that
> transparently encoded Unicode objects with the chosen encoding. If
> such a method is not implemented (I haven't seen one) what would be a 
> good
> way to solve this problem.

It's simple enough to implement. Here is what quotient does:

     def render(self, request):
         request.setHeader("Content-type", 'text/html; charset=UTF-8')

         return renderer.Renderer.render(self, request)

And then, when it wants to present a unicode string, it does:

theStr.encode('utf8')

dp