3

Trying to enable login in flask with flask-security. The following code works fine (I import in __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())

If, however, I change user = Blueprint('user', __name__) to user = Blueprint('user', __name__, url_prefix='/blah') it fails with a werkzeug.routing.BuildError BuildError: ('auth.authenticate', {}, None). url_for is the problem, however, I don't understand why. HALP!

4

2 回答 2

8

弄清楚了。原来 Flask-security 已经/login /logout内置,我的登录功能在很大程度上被忽略了。在正常的应用程序执行期间未使用它。除了url_for仍然被处理,因此 BuildError。

我试图使用来自https://github.com/dracule/FlaskBootstrapSecurity的过时示例,但失败了。Flask-Security 已于 3 个月前更新。动作参数应该更新为action=url_for('security.login')并且它将起作用。

TL;DR 添加一个url_prefix不会使用 Flask-Security 的内置/login /logout并且很多 vars 被重命名

于 2012-12-26T17:12:35.110 回答
0

我知道这是一个老问题,但以防万一有人像我一样遇到这个问题,这里是相关文档

于 2020-03-06T15:23:07.010 回答