使用FlaskSecurity进行Flask身份验证

2024-10-01 11:26:51 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试使用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)。网址是问题,但是,我不明白为什么。停下!在


Tags: namefromimportappurlflaskforlogin
1条回答
网友
1楼 · 发布于 2024-10-01 11:26:51

明白了。原来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,许多变量都被重命名

相关问题 更多 >