1

我正在尝试在比 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)
4

1 回答 1

0

所以它说“没有这样的桌子”。你检查过你真的有那个表和创建的数据库吗?

像这样写:“pip install flask-migrate”

然后复制粘贴此脚本:

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.script import Manager
from flask.ext.migrate import Migrate, MigrateCommand
from app.models import *
from flask.ext.security import UserMixin, RoleMixin



app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'

db = SQLAlchemy(app)
migrate = Migrate(app, db)

manager = Manager(app)
manager.add_command('db', MigrateCommand)



roles_users = db.Table('roles_users',
                       db.Column('user_id', db.Integer(), db.ForeignKey('users.id')),
                       db.Column('role_id', db.Integer(), db.ForeignKey('roles.id')))


class Role(db.Model, RoleMixin):

    __tablename__ = "roles"

    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(80), unique=True)
    description = db.Column(db.String(255))


class User(db.Model, UserMixin):

    __tablename__ = "users"

    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(255), unique=True)
    password = db.Column(db.String(120))
    active = db.Column(db.Boolean())
    last_login_at = db.Column(db.DateTime())
    current_login_at = db.Column(db.DateTime())
    last_login_ip = db.Column(db.String(100))
    confirmed_at = db.Column(db.DateTime())
    current_login_ip = db.Column(db.String(100))
    login_count = db.Column(db.Integer)
    roles = db.relationship('Role', secondary=roles_users,
                            backref=db.backref('users', lazy='dynamic'))
    connections = db.relationship('Connection',
                                  backref=db.backref('user', lazy='joined'), cascade="all")

    def __str__(self):
        return '<User id=%s email=%s>' % (self.id, self.email)


class Connection(db.Model):

    __tablename__ = "connections"

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    provider_id = db.Column(db.String(255))
    provider_user_id = db.Column(db.String(255))
    access_token = db.Column(db.String(255))
    secret = db.Column(db.String(255))
    display_name = db.Column(db.String(255))
    full_name = db.Column(db.String(255))
    profile_url = db.Column(db.String(512))
    image_url = db.Column(db.String(512))
    rank = db.Column(db.Integer)



if __name__ == '__main__':
    manager.run()

在那之后写:

python db_migrate 数据库初始化

python db_migrate 数据库迁移

python db_migrate 数据库升级

然后用“nano app.db”检查它,你应该看到创建的数据库和表。

于 2014-08-24T19:22:59.447 回答