- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个 Web 应用程序,它使用 Flask、SQLAlchemy 和 WTForms,以及必要的 Flask 扩展以使其全部正常工作。 MySQL 对所有表和列使用 utf8_bin
。
我插入了一些中文字符并且 phpMyAdmin 正确显示了它们,但是每当我尝试打开一个页面时,我都会遇到以下异常:
UnicodeDecodeError:“ascii”编解码器无法解码位置 0 中的字节 0xe6:序号不在范围内 (128)
我知道我应该decode('utf8')
我想显示的字段,但这不应该由 SQLAlchemy 为我处理吗?
我设法完成这项工作的唯一方法是遍历结果列表并执行类似以下操作:
object.property = object.property.decode('utf8')
但显然这不应该是手工完成的。我错过了什么?
更新:SQLAlchemy 映射
class Thread(db.Model):
__tablename__ = 'Thread'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.Unicode(255), nullable=False)
body = db.Column(db.Text, nullable=True)
date_created = db.Column(db.DateTime, nullable=False, default=datetime.now())
created_by = db.Column(db.Integer, ForeignKey(User.id))
user = relationship(User, backref='threads')
display_hash = db.Column(db.Unicode(255), nullable=False, unique=True)
display_name = db.Column(db.Unicode(255), nullable=True)
nsfw = db.Column(db.Boolean, nullable=False, default=False)
last_updated = db.Column(db.DateTime, nullable=False)
def __init__(self, title=None, body=None, category_id=None, display_name=None):
self.title = title
self.body = body
self.category_id = category_id
self.display_name = display_name
self.display_hash = custom_uuid()
self.last_updated = self.date_created
def __repr__(self):
return u'<Thread %r>' % (self.title)
def url_title(self):
""" Generates an ASCII-only slug. """
result = []
for word in _punct_re.split(self.title.lower()):
result.extend(unidecode(word).split())
return unicode(u'-'.join(result))
更新:堆栈跟踪
`127.0.0.1 - - [06/Oct/2013 02:37:15] "GET /index HTTP/1.1" 500 -
Traceback (most recent call last):
File "/Users/homedirectory/.virtualenvs/fruitshow/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/homedirectory/.virtualenvs/fruitshow/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Users/homedirectory/.virtualenvs/fruitshow/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/homedirectory/.virtualenvs/fruitshow/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/Users/homedirectory/.virtualenvs/fruitshow/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/homedirectory/.virtualenvs/fruitshow/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/homedirectory/.virtualenvs/fruitshow/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/homedirectory/.virtualenvs/fruitshow/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/homedirectory/Projects/Assorted/Fruit Show/app/views.py", line 90, in index
return render_template('index.html', threads=threads, pagination=pagination)
File "/Users/homedirectory/.virtualenvs/fruitshow/lib/python2.7/site-packages/flask/templating.py", line 128, in render_template
context, ctx.app)
File "/Users/homedirectory/.virtualenvs/fruitshow/lib/python2.7/site-packages/flask/templating.py", line 110, in _render
rv = template.render(context)
File "/Users/homedirectory/.virtualenvs/fruitshow/lib/python2.7/site-packages/jinja2/environment.py", line 969, in render
return self.environment.handle_exception(exc_info, True)
File "/Users/homedirectory/.virtualenvs/fruitshow/lib/python2.7/site-packages/jinja2/environment.py", line 742, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/homedirectory/Projects/Assorted/Fruit Show/app/templates/index.html", line 1, in top-level template code
{% extends 'base.html' %}
File "/Users/homedirectory/Projects/Assorted/Fruit Show/app/templates/base.html", line 50, in top-level template code
{% block content %}
File "/Users/homedirectory/Projects/Assorted/Fruit Show/app/templates/index.html", line 14, in block "content"
<a href="{{ url_for('new_thread') }}/{{ thread.display_hash|safe }}/{{ thread.url_title()|safe }}">{{ thread.title|safe }}</a>
File "/Users/homedirectory/.virtualenvs/fruitshow/lib/python2.7/site-packages/jinja2/filters.py", line 747, in do_mark_safe
return Markup(value)
File "/Users/homedirectory/.virtualenvs/fruitshow/lib/python2.7/site-packages/markupsafe/__init__.py", line 72, in __new__
return text_type.__new__(cls, base)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 0: ordinal not in range(128)`
更新:项目 repo 的 URL:
最佳答案
问题出在我使用的 MySQL 驱动程序上。
我关注了this回答并将列类型从 utf8_bin
切换到 utf8_general_ci
就可以了。
关于python - Flask、SQLAlchemy 和 Jinja2 - UnicodeDecodeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19186058/
我正在遵循Flask-potion教程并尝试保护我的api。我一直试图找出如何在User端点上设置权限,以使一个User无法查看另一个User的记录。这是我的代码: class UserResourc
项目设置 Python 3.5.3 flask 0.12.2 目录 . ├── Core │ ├── BackgroundProcessManager.py │ ├── FirebaseDat
我正在构建 flask 应用程序并决定这次尝试应用程序工厂方法,但是遇到了 flask 迁移的麻烦并且无法找出简单的解决方案。 请注意,我想将配置位置作为选项传递给脚本 管理.py: manager
我尝试通过以下示例代码来熟悉Flask-APScheduler插件:https://github.com/viniciuschiele/flask-apscheduler/blob/master/ex
我一周前刚刚了解了 Flask 和 JWT,所以我真的需要一些帮助。有人可以解释一下flask-jwt和flask-jwt-extended之间的区别吗? 最佳答案 Flask-JWT 没有那么多功能
我是 Flask 的新手,我只是想用它创建一个简单的市场。我的网站上有三种类型的用户 [卖家、买家、管理员]。我认为每种类型的用户都应该有自己的注册和登录,因为他们的角色应该提供不同类型的信息。另一方
基本上,我想做一些非常简单的事情:我想创建一个用于删除数据库中条目的表单。 模板正在创建一个包含所有条目的html表,没有任何麻烦。我现在的问题是:如何将其转换为每行都有链接的表单。 当然,我可以通过
我对Flask来说非常陌生,其中一个基本要求是我需要SEO友好的URL。 我说一条路线 @app.route('/sales/') @app.route(/sales/) def get_sales(
一直在为用户管理系统开发Flask-user,但是我看不到自定义登录页面和注册页面的方法。我使用pip安装了flask-user pip install flask-user 任何帮助,将不胜感激 最
我有一个奇怪的情况,不知道如何找到问题。 我已经创建了一个这样的蓝图 reportjobsmod = Blueprint('jobreports', __name__, url_prefix='/jo
我认为在 Flask 中拥有实例变量的正确方法是添加用户和 session ,但我正在尝试测试一个概念,我还不想经历所有这些。我试图让一个网络应用程序将图像加载到一个变量中,然后可以对其执行不同的图像
select = SelectField("City") form.select.choices = [(places['name'], places['name']) for places in p
这个问题在这里已经有了答案: Provide extra information to Flask's app.logger (2 个回答) 3年前关闭。 我正在运行一个 flask 应用程序。我想写
我当时正在编程一个非常不错的api,使用json从游戏服务器到我的网站空间获取一些json数据, 但是每次我使用angular发送请求时,我都会得到以下信息:127.0.0.1--[20 / Mar
我已经开发了一个上传表单,以将特定的.xlsx文件作为上传。要求是要处理非xlsx上载的任何异常(例如zip,exe文件)。我正在使用pyexcel库读取上传内容。我尝试创建以下代码来处理此异常: e
我使用此功能在网站上捕获404错误: @app.errorhandler(404) def page_not_found(e): logger.warning('User raised an
我正在尝试同时使用 Flask Restful 和 Flask SocketIO。我已经制作了一个 Flask Restful 应用程序,但现在我想使用 Flask SocketIO 在客户端和我的服
我有一些 Flask 应用程序,我通过设置 FLASK_APP 来运行它们。环境变量和调用 flask run .自 FLASK_DEBUG也设置了,每当我更改一些代码时,应用程序都可以方便地重新加载
我目前正在使用 Flask 构建一个应用程序。我正在努力访问 Flask 实例“app”以及 Flask-mail 实例“mail”。 下面是我的项目的样子: └── my-project ├
Remember-me是 parameter flask_login.login_user(user, remember=False, duration=None, force=False, fres
我是一名优秀的程序员,十分优秀!