我有这段代码https://github.com/italomaia/flask-empty/blob/master/src/0.8/main.py我在文件末尾写了这段代码
def configure_before_request(app):
@app.before_request
def before_request():
hash = pbkdf2_sha256.encrypt(app.config['PASSWORD'], rounds=8000, salt_size=10)
if session.get('logged_in') != hash:
return redirect(url_for('login'))
def configure_views(app):
@app.route('/login/', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
hash = pbkdf2_sha256.encrypt(app.config['PASSWORD'], rounds=8000, salt_size=10)
if request.form['login'] == app.config['USERNAME'] and pbkdf2_sha256.verify(request.form['password'], hash):
session['logged_in'] = hash
return redirect(url_for('index'))
else:
flash(u'Неверный логин или пароль')
return render_template('login.html')
@app.route('/', methods=['GET', 'POST'])
def index():
return 'index_page НАХ.'
如果我运行此代码,我会收到 302 服务器错误(ERR_TOO_MANY_REDIRECTS
),但如果我更改此行return redirect(url_for('login'))
,则return 'Hello!'
所有代码都可以正常工作!我做错了什么?
谢谢。
编辑解决方案问题。
更换
if session.get('logged_in') != hash:
_
if session.get('logged_in') != hash and request.endpoint != 'login':