Flask-WhooshAlchemy raises NameError: name 'unicode' is not defined
Flask-WhooshAlchemy raises NameError: name 'unicode' is not defined
尝试使用 WhooshAlchemy 在 Flask 应用程序中创建搜索,但出现错误。
class Employee(db.Model):
__searchable__ = ['first_name', 'patronymic', 'last_name',
'position', 'office', 'birthday']
id = db.Column(db.Integer, primary_key=True)
first_name = db.Column(db.String(60))
patronymic = db.Column(db.String(60))
last_name = db.Column(db.String(60))
position = db.Column(db.String(60))
office = db.Column(db.String)
birthday = db.Column(db.Date)
def __init__(self, first_name, patronymic, last_name, position,
office, birthday):
self.first_name = first_name
self.patronymic = patronymic
self.last_name = last_name
self.position = position
self.office = office
self.birthday = birthday
def __repr__(self):
return '<First name: {}, Patronymic: {}, Last name: {},
Position: {}, Office: {}, Birthday: {}>'\
.format(self.first_name, self.patronymic, self.last_name,
self.position, self.office, self.birthday)
wa.whoosh_index(app, Employee)
@app.route('/results')
@login_required
def results():
q = request.args.get('q')
if q:
result = Employee.query.whoosh_search('q').all()
else:
result = Employee.query.all()
return render_template('results.html', result=result)
Traceback (most recent call last):
File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask_login/utils.py", line 261, in decorated_view
return func(*args, **kwargs)
File "/Users/vladimir/PycharmProjects/adminFlask/auth/routes.py", line 77, in results
result = Employee.query.whoosh_search('q').all()
File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask_whooshalchemy.py", line 103, in whoosh_search
if not isinstance(query, unicode):
NameError: name 'unicode' is not defined
看起来 Flask-WhooshAlchemy 还没有为 Python3 做好准备,如果我们查看 code on github,我们会发现相关部分:
if not isinstance(query, unicode):
query = unicode(query)
results = self._whoosh_searcher(query, limit, fields, or_)
现在,这是指 unicode
内置类型,在 Python3 中不再可用,请参阅解释器中的此练习,Python2:
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> unicode
<type 'unicode'>
并在 Python3 中执行相同的操作,我们发现我们遇到了您遇到的错误:
Python 3.6.5 (default, Apr 1 2018, 05:46:30)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> unicode
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'unicode' is not defined
我刚刚也解决了这个问题,显然它不兼容python 3.我使用的解决方案是here:
卸载你的 whoosh alchemy 版本:
$ flask/bin/pip uninstall flask-whooshalchemy
从 Miguel Grinberg 安装此版本:
flask/bin/pip install git+git://github.com/miguelgrinberg/flask-whooshalchemy.git
删除您的数据库并重新创建它...它应该可以工作
尝试使用 WhooshAlchemy 在 Flask 应用程序中创建搜索,但出现错误。
class Employee(db.Model):
__searchable__ = ['first_name', 'patronymic', 'last_name',
'position', 'office', 'birthday']
id = db.Column(db.Integer, primary_key=True)
first_name = db.Column(db.String(60))
patronymic = db.Column(db.String(60))
last_name = db.Column(db.String(60))
position = db.Column(db.String(60))
office = db.Column(db.String)
birthday = db.Column(db.Date)
def __init__(self, first_name, patronymic, last_name, position,
office, birthday):
self.first_name = first_name
self.patronymic = patronymic
self.last_name = last_name
self.position = position
self.office = office
self.birthday = birthday
def __repr__(self):
return '<First name: {}, Patronymic: {}, Last name: {},
Position: {}, Office: {}, Birthday: {}>'\
.format(self.first_name, self.patronymic, self.last_name,
self.position, self.office, self.birthday)
wa.whoosh_index(app, Employee)
@app.route('/results')
@login_required
def results():
q = request.args.get('q')
if q:
result = Employee.query.whoosh_search('q').all()
else:
result = Employee.query.all()
return render_template('results.html', result=result)
Traceback (most recent call last):
File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask_login/utils.py", line 261, in decorated_view
return func(*args, **kwargs)
File "/Users/vladimir/PycharmProjects/adminFlask/auth/routes.py", line 77, in results
result = Employee.query.whoosh_search('q').all()
File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask_whooshalchemy.py", line 103, in whoosh_search
if not isinstance(query, unicode):
NameError: name 'unicode' is not defined
看起来 Flask-WhooshAlchemy 还没有为 Python3 做好准备,如果我们查看 code on github,我们会发现相关部分:
if not isinstance(query, unicode):
query = unicode(query)
results = self._whoosh_searcher(query, limit, fields, or_)
现在,这是指 unicode
内置类型,在 Python3 中不再可用,请参阅解释器中的此练习,Python2:
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> unicode
<type 'unicode'>
并在 Python3 中执行相同的操作,我们发现我们遇到了您遇到的错误:
Python 3.6.5 (default, Apr 1 2018, 05:46:30)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> unicode
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'unicode' is not defined
我刚刚也解决了这个问题,显然它不兼容python 3.我使用的解决方案是here:
卸载你的 whoosh alchemy 版本:
$ flask/bin/pip uninstall flask-whooshalchemy
从 Miguel Grinberg 安装此版本:
flask/bin/pip install git+git://github.com/miguelgrinberg/flask-whooshalchemy.git
删除您的数据库并重新创建它...它应该可以工作