我正在尝试在比 Flask-Sec 文档中给出的示例更有条理的应用程序中使用 Flask-Security。
使用格式:
\proj
__init.py__
views.py
models.py
config.py
我在 models.py 中设置了快速入门代码。据我所知,这是一个非常精确的副本。
from proj import db
##################################### USERS AND ROLES ############################################
roles_users = db.Table('roles_users',
db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))
class Role(db.Model, RoleMixin):
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(80), unique=True)
description = db.Column(db.String(250))
class User(db.Model, UserMixin):
id = db.Column(db.Integer(), primary_key=True)
email = db.Column(db.String(255), unique=True)
password = db.Column(db.String(255))
active = db.Column(db.Boolean())
confirmed_at = db.Column(db.DateTime())
roles = db.relationship('Role', secondary=roles_users, backref=db.backref('users'), lazy="dynamic")
#Setup Flask security
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore)
然而,当一切都说完了,我得到了这个回溯:
16:42:04 web.1 | Traceback (most recent call last):
16:42:04 web.1 | File "/Users/user/programming/proj/env/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 126, in handle_request
16:42:04 web.1 | respiter = self.wsgi(environ, resp.start_response)
16:42:04 web.1 | File "/Users/user/programming/proj/env/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
16:42:04 web.1 | return self.wsgi_app(environ, start_response)
16:42:04 web.1 | File "/Users/user/programming/proj/env/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
16:42:04 web.1 | response = self.make_response(self.handle_exception(e))
16:42:04 web.1 | File "/Users/user/programming/proj/env/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
16:42:04 web.1 | reraise(exc_type, exc_value, tb)
16:42:04 web.1 | File "/Users/user/programming/proj/env/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
16:42:04 web.1 | response = self.full_dispatch_request()
16:42:04 web.1 | File "/Users/user/programming/proj/env/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
16:42:04 web.1 | rv = self.handle_user_exception(e)
16:42:04 web.1 | File "/Users/user/programming/proj/env/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
16:42:04 web.1 | reraise(exc_type, exc_value, tb)
16:42:04 web.1 | File "/Users/user/programming/proj/env/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
16:42:04 web.1 | rv = self.dispatch_request()
16:42:04 web.1 | File "/Users/user/programming/proj/env/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
16:42:04 web.1 | return self.view_functions[rule.endpoint](**req.view_args)
16:42:04 web.1 | File "/Users/user/programming/proj/env/lib/python2.7/site-packages/flask_security/views.py", line 116, in register
16:42:04 web.1 | if form.validate_on_submit():
16:42:04 web.1 | File "/Users/user/programming/proj/env/lib/python2.7/site-packages/flask_wtf/form.py", line 156, in validate_on_submit
16:42:04 web.1 | return self.is_submitted() and self.validate()
16:42:04 web.1 | File "/Users/user/programming/proj/env/lib/python2.7/site-packages/wtforms/form.py", line 271, in validate
16:42:04 web.1 | return super(Form, self).validate(extra)
16:42:04 web.1 | File "/Users/user/programming/proj/env/lib/python2.7/site-packages/wtforms/form.py", line 130, in validate
16:42:04 web.1 | if not field.validate(self, extra):
16:42:04 web.1 | File "/Users/user/programming/proj/env/lib/python2.7/site-packages/wtforms/fields/core.py", line 175, in validate
16:42:04 web.1 | stop_validation = self._run_validation_chain(form, chain)
16:42:04 web.1 | File "/Users/user/programming/proj/env/lib/python2.7/site-packages/wtforms/fields/core.py", line 195, in _run_validation_chain
16:42:04 web.1 | validator(form, self)
16:42:04 web.1 | File "/Users/user/programming/proj/env/lib/python2.7/site-packages/flask_security/forms.py", line 81, in unique_user_email
16:42:04 web.1 | if _datastore.find_user(email=field.data) is not None:
16:42:04 web.1 | File "/Users/user/programming/proj/env/lib/python2.7/site-packages/flask_security/datastore.py", line 199, in find_user
16:42:04 web.1 | return self.user_model.query.filter_by(**kwargs).first()
16:42:04 web.1 | File "/Users/user/programming/proj/env/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2278, in first
16:42:04 web.1 | ret = list(self[0:1])
16:42:04 web.1 | File "/Users/user/programming/proj/env/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2145, in __getitem__
16:42:04 web.1 | return list(res)
16:42:04 web.1 | File "/Users/user/programming/proj/env/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2349, in __iter__
16:42:04 web.1 | return self._execute_and_instances(context)
16:42:04 web.1 | File "/Users/user/programming/proj/env/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2364, in _execute_and_instances
16:42:04 web.1 | result = conn.execute(querycontext.statement, self._params)
16:42:04 web.1 | File "/Users/user/programming/proj/env/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 662, in execute
16:42:04 web.1 | params)
16:42:04 web.1 | File "/Users/user/programming/proj/env/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 761, in _execute_clauseelement
16:42:04 web.1 | compiled_sql, distilled_params
16:42:04 web.1 | File "/Users/user/programming/proj/env/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 874, in _execute_context
16:42:04 web.1 | context)
16:42:04 web.1 | File "/Users/user/programming/proj/env/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1024, in _handle_dbapi_exception
16:42:04 web.1 | exc_info
16:42:04 web.1 | File "/Users/user/programming/proj/env/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 195, in raise_from_cause
16:42:04 web.1 | reraise(type(exception), exception, tb=exc_tb)
16:42:04 web.1 | File "/Users/user/programming/proj/env/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 867, in _execute_context
16:42:04 web.1 | context)
16:42:04 web.1 | File "/Users/user/programming/proj/env/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 324, in do_execute
16:42:04 web.1 | cursor.execute(statement, parameters)
16:42:04 web.1 | OperationalError: (OperationalError) no such table: user u'SELECT user.id AS user_id, user.email AS user_email, user.password AS user_password, user.active AS user_active, user.confirmed_at AS user_confirmed_at \nFROM user \nWHERE user.email = ?\n LIMIT ? OFFSET ?' (u'user.@gmail.com', 1, 0)