Hello, i would like to feed a zpt form with data from a python dictionary via a python script zpt snippet: <textarea name="title:record:ulines" rows="3" cols="65"> <tal:span content="python:context.printDict(here.dict)"/> </textarea> python script prinDict: keys_ = dict.keys() keys_.sort() lines=[] for key in keys_: lines.append(u"%s:%s\n" %(key ,dict[key])) uni_str = u"".join(lines) return uni_str.encode('utf-8') I'm always getting a UnicodeDecodeError. I' using Zope 2.7.5, CMF 1.5.1, Python 2.3.5 HTML meta tag is set to <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> CMF default_charset = utf-8 Regards Joern
--On Montag, 23. Mai 2005 10:35 Uhr +0200 Joern Wallstabe <joern.wallstabe@igd-r.fraunhofer.de> wrote:
I'm always getting a UnicodeDecodeError.
To every error message belongs a traceback where you can see *where* the error occurs...another try? -aj
Andreas Jung wrote:
--On Montag, 23. Mai 2005 10:35 Uhr +0200 Joern Wallstabe <joern.wallstabe@igd-r.fraunhofer.de> wrote:
I'm always getting a UnicodeDecodeError.
To every error message belongs a traceback where you can see *where* the error occurs...another try?
-aj
the script decodes unicode 'Spécification' -> utf8 'Sp\xc3\xa9cification' the error is raised during zpt processing. by the way python default default encoding is set to ascii. Traceback (innermost last): Module ZPublisher.Publish, line 101, in publish Module ZPublisher.mapply, line 88, in mapply Module ZPublisher.Publish, line 39, in call_object Module Products.CMFFormController.FSControllerPageTemplate, line 98, in __call__ Module Products.CMFFormController.BaseControllerPageTemplate, line 42, in _call Module Shared.DC.Scripts.Bindings, line 306, in __call__ Module Shared.DC.Scripts.Bindings, line 343, in _bindAndExec Module Products.CMFCore.FSPageTemplate, line 188, in _exec Module Products.CMFCore.FSPageTemplate, line 127, in pt_render Module Products.PageTemplates.PageTemplate, line 97, in pt_render <FSControllerPageTemplate at /cmf/edit_form used for /cmf/Members/wal/myid> Module StringIO, line 203, in getvalue UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 95: ordinal not in range(128) regards joern
--On Montag, 23. Mai 2005 11:18 Uhr +0200 Joern Wallstabe <joern.wallstabe@igd-r.fraunhofer.de> wrote:
the script decodes unicode 'Spécification' -> utf8 'Sp\xc3\xa9cification'
the error is raised during zpt processing. by the way python default default encoding is set to ascii.
hm...I can't reproduce this behaviour :-/ ANdreas
Joern Wallstabe wrote at 2005-5-23 11:18 +0200:
... the error is raised during zpt processing. by the way python default default encoding is set to ascii. .... <FSControllerPageTemplate at /cmf/edit_form used for /cmf/Members/wal/myid> Module StringIO, line 203, in getvalue UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 95: ordinal not in range(128)
Your page template mixes unicode and non-unicode (which cannot be decoded with the default encoding). -- Dieter
Am Montag, den 23.05.2005, 10:35 +0200 schrieb Joern Wallstabe:
Hello,
i would like to feed a zpt form with data from a python dictionary via a python script
zpt snippet:
<textarea name="title:record:ulines" rows="3" cols="65"> <tal:span content="python:context.printDict(here.dict)"/>
This is wrong anyway. Use tal:content in the textarea instead.
</textarea>
python script prinDict:
keys_ = dict.keys() keys_.sort() lines=[]
for key in keys_: lines.append(u"%s:%s\n" %(key ,dict[key]))
uni_str = u"".join(lines) return uni_str.encode('utf-8')
I'm always getting a UnicodeDecodeError.
I' using Zope 2.7.5, CMF 1.5.1, Python 2.3.5 HTML meta tag is set to <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> CMF default_charset = utf-8
meta charset does not count btw. CMF default should, but it really depends on the real content-type header you send. Make sure its the encoding you want there (setHeader() ) And its important to watch the encoding of the strings you work with. Where does the dictionary come from?
Thank you all for replying - now it works I got utf-8 string in my form. Seem Zopes ZPT system is trying to do the encoding, depending on on setHeader() method. So if this header is set you can simply return unicode strings. Strange setting cmf default charset to utf-8 doesn't work :-(
Am Montag, den 23.05.2005, 10:35 +0200 schrieb Joern Wallstabe:
Hello,
i would like to feed a zpt form with data from a python dictionary via a python script
zpt snippet:
<textarea name="title:record:ulines" rows="3" cols="65"> <tal:span content="python:context.printDict(here.dict)"/>
This is wrong anyway. Use tal:content in the textarea instead.
o.k. replace that with <tal:content content="python:context.printDict(here.dict)"/>
</textarea>
python script prinDict:
keys_ = dict.keys() keys_.sort() lines=[]
for key in keys_: lines.append(u"%s:%s\n" %(key ,dict[key]))
uni_str = u"".join(lines) return uni_str.encode('utf-8')
function now returns unicode string without utf-8 encoding
I'm always getting a UnicodeDecodeError.
I' using Zope 2.7.5, CMF 1.5.1, Python 2.3.5 HTML meta tag is set to <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> CMF default_charset = utf-8
meta charset does not count btw. CMF default should, but it really depends on the real content-type header you send. Make sure its the encoding you want there (setHeader() )
That was the important hint: added to cmf main_header_template: tal:define " dummy python: request.RESPONSE.setHeader('content-type', 'text/html;; charset=utf-8');
And its important to watch the encoding of the strings you work with. Where does the dictionary come from?
python dictionary was result form parsing xml data into that dictionary (all unicode) Thanx again :-)
Joern Wallstabe wrote:
Seem Zopes ZPT system is trying to do the encoding, depending on on setHeader() method. So if this header is set you can simply return unicode strings.
It's actually lower down in the stack... ZPublisher encodes the response, and has a stoopid hard coded default of 'latin-1' that it returns ;-) cheers, Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk
Chris Withers wrote at 2005-5-25 20:54 +0100:
Joern Wallstabe wrote:
Seem Zopes ZPT system is trying to do the encoding, depending on on setHeader() method. So if this header is set you can simply return unicode strings.
It's actually lower down in the stack... ZPublisher encodes the response, and has a stoopid hard coded default of 'latin-1' that it returns ;-)
You are wrong: ZPublisher uses what it finds in the "charset" parameter of the "Content-Type" response header. Only the default is "latin-1" (as specified by the HTML specification). -- Dieter
Dieter Maurer wrote:
Only the default is "latin-1" (as specified by the HTML specification).
Indeed, but that should be configurable... Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk
participants (5)
-
Andreas Jung -
Chris Withers -
Dieter Maurer -
Joern Wallstabe -
Tino Wildenhain