【问题标题】:Flask not redirecting to "next" query string烧瓶未重定向到“下一个”查询字符串
【发布时间】:2021-11-27 18:52:00
【问题描述】:

以下是我的登录功能的代码(简化)。
我正在尝试从 url 获取“下一个”参数以将用户重定向到。但它不起作用。

@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 始终为空。谢谢你的帮助。

【问题讨论】:

  • 您能否提供一个无效的 URL 示例?

标签: flask flask-login


【解决方案1】:

问题是“下一个”参数没有在发布请求中传输。我最终在登录页面上做了这样的事情:

      <!-- Hidden input to store the next argument -->
      <input
          type="hidden"
          name="next"
          value="{{ request.args.get('next', '') }}"
      />

在 Flask 路由中,我从以下位置检索下一个参数: next_page = request.args.get('next')

愚蠢的错误,我的错。

【讨论】:

    猜你喜欢
    • 2018-02-18
    • 2014-12-03
    • 1970-01-01
    • 2017-09-26
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多