下面是我的登錄功能的代碼(簡化)。
我正在嘗試從 url 獲取“next”引數以將用戶重定向到。但它不起作用。
@bp.route('/login', methods=['GET', 'POST'])
def login():
if current_user.is_authenticated:
return redirect(url_for('main.dashboard'))
if request.method == 'POST':
form_data = request.form
email = form_data.get('email')
password = form_data.get('password')
... (Assume here that the user logged in)
login_user(user, remember=True)
next_page = request.args.get('next')
if not next_page or url_parse(next_page).netloc != '':
next_page = url_for('main.dashboard')
return redirect(next_page)
return render_template('auth/login.html', title='Login')
在上面的代碼中,next_page 始終為空。感謝您的幫助。
uj5u.com熱心網友回復:
問題是“下一個”引數未在發布請求中傳輸。我最終在登錄頁面上做了這樣的事情:
<!-- Hidden input to store the next argument -->
<input
type="hidden"
name="next"
value="{{ request.args.get('next', '') }}"
/>
在 Flask 路由中,我從以下位置檢索下一個引數:
next_page = request.args.get('next')
愚蠢的錯誤,我的錯。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/372067.html
上一篇:Flask表單提交成功和錯誤
