首页 > 解决方案 > 页面加载错误上的 Flask 发布请求

问题描述

每当我刷新我的页面时,都会在我的控制台中看到一个发布请求。这意味着它会在我输入任何内容或单击任何按钮之前请求我的表单。我需要将它传递给 Jinja 并通过 js,但是,它总是返回 0,因为变量用户名或密码中没有数据。

@app.route('/dosomething', methods = ['POST', 'GET'])
def dosomething():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        check = system.add_user(username,password)
        if check == 1:
            return redirect(url_for("login"))
        return render_template('dosomething.html', check = check) 
    return render_template('dosomething.html') 

标签: javascriptpythonhtmlflask

解决方案


您必须使用 307 http 代码重定向。

if check == 1:
    return redirect(url_for('login'), code=307)

推荐阅读