Plone模板抱怨UnicodeCodeError(ascii),但默认的zpublisher编码是utf-8
我有两个Plone实例(在独立的Zope实例中),它们的代码基本相同,包括搜索;两者都配置为使用Plone模板抱怨UnicodeCodeError(ascii),但默认的zpublisher编码是utf-8,utf-8,character-encoding,plone,plone-4.x,Utf 8,Character Encoding,Plone,Plone 4.x,我有两个Plone实例(在独立的Zope实例中),它们的代码基本相同,包括搜索;两者都配置为使用utf-8,都与sys.getdefaultcoding和defaultzpublisher编码有关 现在,在其中一个站点中,在呈现搜索结果期间,搜索某些目录搜索失败,可能是因为搜索结果中存在umlauts: UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1: ordinal not in range(128)
utf-8
,都与sys.getdefaultcoding
和defaultzpublisher编码有关
现在,在其中一个站点中,在呈现搜索结果期间,搜索某些目录搜索失败,可能是因为搜索结果中存在umlauts:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1: ordinal not in range(128)
HTML是通过调用一个经过utf-8
-编码(在两个系统中都是相同的)的模板创建的,我已经将两个系统配置为在任何地方都使用utf-8
(我是否错过了查找的地方?),那么为什么要将任何内容编码到ascii
我可以使用utf-8
强制执行模板吗,或者干净的解决方案是什么(除了为python3重写的Plone)
谢谢大家!
编辑:
以下是回溯:
Traceback (innermost last):
Module ZPublisher.Publish, line 138, in publish
Module ZPublisher.mapply, line 77, in mapply
Module ZPublisher.Publish, line 48, in call_object
Module Shared.DC.Scripts.Bindings, line 322, in __call__
Module Shared.DC.Scripts.Bindings, line 359, in _bindAndExec
Module Products.CMFCore.FSPageTemplate, line 237, in _exec
Module Products.CMFCore.FSPageTemplate, line 177, in pt_render
Module Products.PageTemplates.PageTemplate, line 79, in pt_render
Module zope.pagetemplate.pagetemplate, line 132, in pt_render
Module zope.pagetemplate.pagetemplate, line 240, in __call__
Module zope.tal.talinterpreter, line 271, in __call__
Module zope.tal.talinterpreter, line 343, in interpret
Module zope.tal.talinterpreter, line 888, in do_useMacro
(... deleted 42 lines ...)
Module zope.tal.talinterpreter, line 513, in no_tag
Module zope.tal.talinterpreter, line 343, in interpret
Module zope.tal.talinterpreter, line 742, in do_insertStructure_tal
Module Products.PageTemplates.Expressions, line 218, in evaluateStructure
Module zope.tales.tales, line 696, in evaluate
- URL: file:/opt/zope/instances/plone2/Products/myproject/skins/myproject_templates/search_view.pt
- Line 176, Column 16
- Expression: <PythonExpr search.result()>
- Names:
{'container': <PloneSite at /vdz>,
'context': <PloneSite at /vdz>,
'default': <object object at 0x7fcdb035f4f0>,
'here': <PloneSite at /vdz>,
'loop': {},
'nothing': None,
'options': {'args': ()},
'repeat': <Products.PageTemplates.Expressions.SafeMapping object at 0x7fcd688ebfc8>,
'request': <HTTPRequest, URL=https://www.myprojectsite.com:/search_view>,
'root': <Application at >,
'template': <FSPageTemplate at /vdz/search_view>,
'traverse_subpath': [],
'user': <SpecialUser 'Anonymous User'>}
Module Products.PageTemplates.ZRPythonExpr, line 48, in __call__
- __traceback_info__: search.result()
Module PythonExpr, line 1, in <expression>
Module Products.myproject.browser.myproject_search.browser, line 433, in result
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1: ordinal not in range(128)
执行搜索的被调用的searchQs
方法确保搜索表达式使用unicode字符串(通过safe_decode
函数,该函数按此顺序尝试unicode(未更改),utf-8
和latin-1
)
一开始我没有设置项目;也许我会尝试在不更改sys.defaultencoding
的情况下实现它。现在这是必需的
是的,我使用的是virtualenv
,在那个环境中更改默认编码需要一些努力;项目发展了一段时间,从一开始就不是基于buildout
和vitualenv
。您是否能够在数据库的非生产副本上进行试验?(一时兴起,我会说ZMI,portal_目录,高级,清除和重建不会受伤。)是的,我可以;但我还不能在我的开发副本中重现这个问题。。。我将在同一台机器上的另一个副本中重试。您使用的是virtualenv吗?也许您通过老式的sitecustomize.py或similarsys进行了一些奇怪的区域设置编码自定义。getdefaultencoding通常是“ascii”。显然,将其设置为utf-8在某些情况下会有所帮助,但可能会导致其他意外问题。不建议这样做。至少,它会导致在您的机器上运行良好的代码在使用ascii作为默认编码的机器上失败。另外,你能发布一个完整的回溯吗?这将有助于找出问题所在。我们可以看到整个堆栈跟踪吗?您是否能够在数据库的非生产副本上进行实验?(一时兴起,我会说ZMI,portal_目录,高级,清除和重建不会受伤。)是的,我可以;但我还不能在我的开发副本中重现这个问题。。。我将在同一台机器上的另一个副本中重试。您使用的是virtualenv吗?也许您通过老式的sitecustomize.py或similarsys进行了一些奇怪的区域设置编码自定义。getdefaultencoding通常是“ascii”。显然,将其设置为utf-8在某些情况下会有所帮助,但可能会导致其他意外问题。不建议这样做。至少,它会导致在您的机器上运行良好的代码在使用ascii作为默认编码的机器上失败。另外,你能发布一个完整的回溯吗?这将有助于查明问题所在。我们能看到整个堆栈跟踪吗?
def result(self):
"""
Sadly no useful docstring ...
"""
context = self.context
res = {'brains': ''}
try:
res = {'brains': self.searchQs()}
string_ = context.restrictedTraverse('kss-search-result')(**res)
except Unauthorized:
string_ = "Unauthorized"
except Exception, e:
raise e