gpt4 book ai didi

python - Flask-Security无法正确初始化

转载 作者:行者123 更新时间:2023-12-03 16:46:04 24 4
gpt4 key购买 nike

编写了自己的登录/注册系统后,我发现了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-ExampleFlask-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'))
)

最佳答案

看起来我发现了我的问题(facepalm):

security = Security(app)


我应该将数据存储区作为第二个参数传递:

security = Security(app, user_datastore)

关于python - Flask-Security无法正确初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14802445/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com