我正在尝试使用基本的login_required方法保护我的页面。这对于主页(仪表板)工作正常,但对于已加载的蓝图则工作不正常。还有保护他们的方法吗? 我想保持我的应用程序模块化,而不是将所有代码都存储在主应用程序中。
我有以下内容:
app = flask.Flask(__name__)
app.secret_key = 'T0pS3cr3tKeY!'
def login_required(f):
@wraps(f)
def wrap(*args, **kwargs):
if 'logged_in' in flask.session:
return f(*args, **kwargs)
else:
flask.flash('You need to login first!')
return flask.redirect(flask.url_for('/'))
return wrap
@app.route('/', methods=['GET', 'POST'])
def login():
error = None
if flask.request.method == 'POST':
if flask.request.form['email'] != 'admin' or flask.request.form['password'] != 'admin':
error = 'Invalid login details, please try again.'
else:
flask.session['logged_in'] = True
flask.flash('You were just logged in!')
return flask.redirect(flask.url_for('dashboard'))
return flask.render_template('login.html', error = error)
@app.route('/dashboard')
@login_required
def dashboard():
return flask.render_template('index.html')
@app.route('/logout')
def logout():
flask.session.pop('logged_in', None)
flask.flash('You were just logged out!')
return flask.redirect(flask.url_for('login'))
app.register_blueprint(app1)
app.register_blueprint(app2)
app.register_blueprint(app3)