2009-07-16 4 views
2

구글 앱 엔진 템플릿 유니 코드 디코딩 문제

from google.appengine.ext.webapp import template

templatepath = os.path.join(os.path.dirname(file), 'template.html')
self.response.out.write (template.render(templatepath , template_values))

나는 다음과 같은 에러를 본적이

<type 'exceptions.UnicodeDecodeError'>: 'ascii' codec can't decode byte 0xe2 in position 17692: ordinal not in range(128)
args = ('ascii', '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Str...07/a-beautiful-method-to-find-peace-of-mind/ -->
', 17692, 17693, 'ordinal not in range(128)')
encoding = 'ascii'
end = 17693
message = ''
object = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Str...07/a-beautiful-method-to-find-peace-of-mind/ -->
reason = 'ordinal not in range(128)'
start = 17692

것 같다 그 기본 장고 템플릿 엔진은 "ascii"인코딩을 가정하며 "utf-8"이어야합니다. 문제의 원인과 해결 방법을 알고있는 사람은 누구입니까? 감사합니다. .

+1

DEFAULT_CHARSET의 값은 무엇입니까? 도움이 될 수 있습니다. – lavinio

답변

6

음과

template.render(templatepath , template_values)

를 교체 템플릿에 의해 반환 된 렌더링 결과를 밝혀하려면 먼저 디코딩 할 필요가 :

self.response.out.write (template.render(templatepath , template_values).decode('utf-8'))

바보 같은 실수는 있지만 어쨌든 모든 사람의 대답을위한 행크스. :)

1

템플릿이 utf-8로 인코딩되었음을 텍스트 편집기에서 확인 했습니까?

2

Django 0.96 또는 Django 1.0을 사용하고 있습니까? 당신이 장고 1.0을 사용하는 경우, FILE_CHARSET 및 DEFAULT_CHARSET 모두 'UTF-8'기본합니다

 
from google.appengine.dist import use_library 
use_library('django', '1.0')

: 그것은 다음이 포함 된 경우 당신은 당신의 main.py를 찾아 봄으로써 확인할 수 있습니다. 템플릿이 다른 인코딩으로 저장된 경우 FILE_CHARSET을 그 값으로 설정하면됩니다.

Django 0.96을 사용하는 경우 디스크에서 템플릿을 직접 읽은 다음 인코딩을 수동으로 처리 할 수 ​​있습니다.

예를 들어,

Template(unicode(template_fh.read(), 'utf-8')).render(template_values)