即使指定,SqlAlchemy也不使用utf8字符集

时间:2014-03-17 16:45:05

标签: python mysql unicode flask flask-sqlalchemy

我在Flask-SqlAlchemy和MySQL上使用Flask,我的连接字符串在我的连接字符串上确实有charset-utf8参数。

我也使用反射,我的模型被指定为:

class Value(db.Model):
    __bind_key__ = 'values'
    __tablename__ = 'values'

我也尝试过将# -*- coding:utf8 -*-放入我的文件中,但我仍然得到这些堆栈跟踪:

Traceback (most recent call last):
  File "/home/numkem/src/sd/venv/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/numkem/src/sd/venv/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/home/numkem/src/sd/venv/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/numkem/src/sd/venv/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/numkem/src/sd/venv/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/numkem/src/sd/venv/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/numkem/src/sd/venv/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/numkem/src/sd/venv/lib/python2.7/site-packages/flask_debugtoolbar/__init__.py", line 125, in dispatch_request
    return view_func(**req.view_args)
  File "/usr/lib/python2.7/cProfile.py", line 149, in runcall
    return func(*args, **kw)
  File "/home/numkem/src/sd/application/views/player.py", line 54, in player_show
    return render_template('player/show.html', **locals())
  File "/home/numkem/src/sd/venv/lib/python2.7/site-packages/flask/templating.py", line 128, in render_template
    context, ctx.app)
  File "/home/numkem/src/sd/venv/lib/python2.7/site-packages/flask/templating.py", line 110, in _render
    rv = template.render(context)
  File "/home/numkem/src/sd/venv/lib/python2.7/site-packages/jinja2/environment.py", line 969, in render
    return self.environment.handle_exception(exc_info, True)
  File "/home/numkem/src/sd/venv/lib/python2.7/site-packages/jinja2/environment.py", line 742, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/numkem/src/sd/application/templates/player/show.html", line 1, in top-level template code
    {% extends "base.html" %}
  File "/home/numkem/src/sd/application/templates/base.html", line 62, in top-level template code
    {% block body %}{% endblock %}
  File "/home/numkem/src/sd/application/templates/player/show.html", line 27, in block "body"
    <td>{{ field.values }}</td>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in position 16: ordinal not in range(128)

为什么连接时会忽略字符集?不应该通过连接字符串设置吗?也许它不是在使用反射时可能/支持的东西。

谢谢!

2 个答案:

答案 0 :(得分:1)

事实证明问题并非来自SqlAlchemy,而是来自Jinja2本身,因为它没有看到我想通过应用程序使用utf8编码。 documentation表示如果未指定任何内容,则默认编码将设置为ascii。

answer显示了我的问题的解决方案。

修复方法是将其添加到您的第一个代码中:

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

我不是100%熟悉python,但我认为这可能与使用WSGI应用程序而不是标准脚本有关,因此# -*- coding:utf8 -*-技巧不起作用。

答案 1 :(得分:0)

此错误意味着您将模板中的非unicode字符串作为上下文传递,因为Jinja2仅支持unicode。

您应该阅读有关编码的material