Flask自动重定向到/

时间:2018-01-22 02:39:37

标签: python html flask request backend

我尝试将表单数据提交给我编写的处理函数,但Flask会自动重定向到' /'这导致400 Bad Request错误。我内有

<form action="{{url_for(createacc())}}" method="POST">

和createacc是

@app.route('/createacc', methods=['GET', 'POST'])
def createacc():
    if request.method == 'POST':
        name = request.form.get['nameinput']
        print("This is from the main file")

        username = request.form['usernameinput']
        passwd = request.form['passwdinput']
        email = request.form['emailinput']
        credentialhandler.add_user(name, username, email, passwd)
        return redirect(url_for(login()), code=302)

    return render_template("createacc.html")

感谢您的帮助!

编辑:如果我添加网站HTML可能会有所帮助

<div class="row">
        <div class="col-md-1"></div>
        <div class="col-md-6">
            <ul style="padding: 3em;">
                <form action="{{url_for(createacc())}}">
                    <div class="form-group">
                            <label for="nameinput">Name</label>
                            <input type="text" class="form-control" id="nameinput" name="nameinput" placeholder="Enter Name">
                    </div>
                    <div class="form-group">
                            <label for="usernameinput">Username</label>
                            <input type="text" class="form-control" id="usernameinput" name="usernameinput" placeholder="Enter Email">
                    </div>
                    <div class="form-group">
                        <label for="emailinput">Email Address</label>
                        <input type="email" class="form-control" id="emailinput" name="emailinput" aria-describedby="emailHelp" placeholder="Enter Email">
                    </div>
                    <div class="form-group">
                        <label for="passwdinput">Password</label>
                        <input type="password" class="form-control" id="passwdinput" name="passwdinput" placeholder="Enter Password">
                    </div>
                    <button type="submit" class="btn btn-primary">Submit</button>
                </form>
            </ul>
        </div>
        <div class="col-md-5"></div>
    </div>

1 个答案:

答案 0 :(得分:0)

尝试将此用于form

<form method="POST" action="{{ url_for('createacc') }}">

return redirect(url_for('login'))

另外

  

重定向状态代码。默认为302。