在编写了自己的登录/注册系统后,我发现了Flask-Security并决定将其集成到我的应用程序中。我所有的身份验证和恢复代码都消失了,但我无法弄清楚为什么在启动我的应用程序时会出现简单的错误。
这是我能想到的最精简的例子:
from flask import Flask, render_template_string
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.security import Security, SQLAlchemyUserDatastore, UserMixin, RoleMixin, current_user
app = Flask('test')
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/database.db'
db = SQLAlchemy(app)
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(255))
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(50), unique=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'))
def is_authenticated(self):
return True
def is_active(self):
return True
def is_anonymous(self):
return False
def get_id(self):
return unicode(self.id)
def __repr__(self):
return '<User {self.username}>'.format(self=self)
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app)
@app.before_first_request
def create_db():
db.create_all()
@app.route('/')
def index():
return render_template_string('''
{% if current_user.is_authenticated() %}
You're logged in.
{% else %}
You're not logged in.
{% endif %}
''')
if __name__ == '__main__':
app.run(debug=True)
访问http://localhost:5000/
时,我收到此错误:
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/flask/app.py", line 1701, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/lib/python2.7/site-packages/flask/app.py", line 1689, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/lib/python2.7/site-packages/flask/app.py", line 1687, in wsgi_app
response = self.full_dispatch_request()
File "/usr/lib/python2.7/site-packages/flask/app.py", line 1360, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/lib/python2.7/site-packages/flask/app.py", line 1358, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/lib/python2.7/site-packages/flask/app.py", line 1344, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/tmp/app.py", line 62, in index
''')
File "/usr/lib/python2.7/site-packages/flask/templating.py", line 140, in render_template_string
context, ctx.app)
File "/usr/lib/python2.7/site-packages/flask/templating.py", line 107, in _render
rv = template.render(context)
File "/usr/lib/python2.7/site-packages/jinja2/environment.py", line 894, in render
return self.environment.handle_exception(exc_info, True)
File "<template>", line 2, in top-level template code
File "/usr/lib/python2.7/site-packages/jinja2/environment.py", line 372, in getattr
return getattr(obj, attribute)
UndefinedError: 'current_user' is undefined
我查看了Flask-Security-Example和Flask-Social-Example的来源,但我不知道我在哪里出错了。
非常感谢所有帮助。
作为一个小问题,为什么这个表是这样创建的,(更重要的是)它是做什么用的?
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'))
)