Форум сайта python.su
вот код: в шаблоне русский текст в utf-8
# coding: utf-8
from bottle import jinja2_template as template
print template('test1')
>python test1.py
Traceback (most recent call last):
File "test1.py", line 5, in <module>
print template('test1')
File "/usr/local/lib/python2.6/dist-packages/bottle-0.6.4-py2.6.egg/bottle.py", line 974, in jinja2_template
return template(tpl_name, **kargs)
File "/usr/local/lib/python2.6/dist-packages/bottle-0.6.4-py2.6.egg/bottle.py", line 955, in template
TEMPLATES[tpl] = template_adapter(name=tpl, lookup=lookup)
File "/usr/local/lib/python2.6/dist-packages/bottle-0.6.4-py2.6.egg/bottle.py", line 758, in __init__
self.prepare()
File "/usr/local/lib/python2.6/dist-packages/bottle-0.6.4-py2.6.egg/bottle.py", line 834, in prepare
self.tpl = self.env.get_template(self.filename)
File "/usr/local/lib/python2.6/dist-packages/Jinja2-2.2.1-py2.6.egg/jinja2/environment.py", line 552, in get_template
template = self.loader.load(self, name, self.make_globals(globals))
File "/usr/local/lib/python2.6/dist-packages/Jinja2-2.2.1-py2.6.egg/jinja2/loaders.py", line 108, in load
code = environment.compile(source, name, filename)
File "/usr/local/lib/python2.6/dist-packages/Jinja2-2.2.1-py2.6.egg/jinja2/environment.py", line 435, in compile
source = self._parse(source, name, filename)
File "/usr/local/lib/python2.6/dist-packages/Jinja2-2.2.1-py2.6.egg/jinja2/environment.py", line 378, in _parse
return Parser(self, source, name, filename).parse()
File "/usr/local/lib/python2.6/dist-packages/Jinja2-2.2.1-py2.6.egg/jinja2/parser.py", line 30, in __init__
self.stream = environment._tokenize(source, name, filename, state)
File "/usr/local/lib/python2.6/dist-packages/Jinja2-2.2.1-py2.6.egg/jinja2/environment.py", line 409, in _tokenize
source = self.preprocess(source, name, filename)
File "/usr/local/lib/python2.6/dist-packages/Jinja2-2.2.1-py2.6.egg/jinja2/environment.py", line 403, in preprocess
self.extensions.itervalues(), unicode(source))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 0: ordinal not in range(128)
Отредактировано (Янв. 26, 2010 18:47:55)
Офлайн
подрубил джинжу отдельно - все нормально работает, видимо в боттле как-то криво интегрировано с темплейтами ( интегрированное мако тоже не робит )
@route('/2')
def main2():
response.content_type = 'text/html; charset=utf-8'
from jinja2 import Environment, FileSystemLoader
env = Environment(loader=FileSystemLoader('views'))
return env.get_template('test1.tpl').render()
Отредактировано (Янв. 27, 2010 06:24:46)
Офлайн
все, я нашел баг в библиотеке Bottle 0.6.4:
class Jinja2Template(BaseTemplate):
def loader(self, name):
if not name.endswith(".tpl"):
for path in self.lookup:
fpath = os.path.join(path, name+'.tpl')
if os.path.isfile(fpath):
name = fpath
break
f = open(name)
try: return f.read()
finally: f.close()
try: return f.read().decode('utf-8')
Офлайн
все, баг фикс закомитили в репозитарий и поблагодарили :)
http://github.com/defnull/bottle/commit/1625213cac575aa796e71a0f5bc7018bb9fa84c6
Офлайн