尝试使用flask security在flask中启用登录。以下代码工作正常(我在__init__.py
中导入)
from flask import Blueprint, render_template, request, redirect, url_for
from coursly import app
from coursly.models import *
from flask.ext.security import Security, LoginForm, SQLAlchemyUserDatastore
user_datastore = SQLAlchemyUserDatastore(db, user.User, user.Role)
Security(app, user_datastore)
user = Blueprint('user', __name__)
@user.route('/')
def index():
return redirect(url_for("user.login"))
@user.route('/login', methods=['GET', 'POST'])
def login():
return render_template('user/login.html', content='Login Page', action=url_for('auth.authenticate'), form=LoginForm())
但是,如果我将user = Blueprint('user', __name__)
改为user = Blueprint('user', __name__, url_prefix='/blah')
,它将失败,并返回werkzeug.routing.BuildError BuildError: ('auth.authenticate', {}, None)
。网址是问题,但是,我不明白为什么。停下!在
明白了。原来Flask security已经内置了
/login
/logout
,我的登录功能基本上被忽略了。在正常的应用程序执行期间没有使用它。除非url_for
仍在处理,因此生成错误。在我试图使用https://github.com/dracule/FlaskBootstrapSecurity中过时的示例,但失败了。烧瓶安全在3个月前更新。action参数应该更新为
action=url_for('security.login')
,并且它会工作。在TL;DR添加
url_prefix
将不会使用Flask Security内置的/login
/logout
,许多变量都被重命名相关问题 更多 >
编程相关推荐