在Flask中,request.method的默认设置为GET

时间:2019-09-11 11:14:00

标签: python post flask

我已经在Flask中创建了一个表单,并希望提交某些需要处理的值。但是,即使我在表单中将方法指定为post,但使用的方法仍默认为GET

这些是相关的代码文件: app.py

@app.route('/test',methods=["GET","POST"])
def test():
    print(request.method)
    error = None
    try:

        if request.method == "POST":

            first_name = request.form['firstname']
            last_name = request.form['lastname']
            flash(first_name)
            flash(last_name)


            return render_template("test.html")
        else:

            return "Wrong"

    except Exception as e:

        return str(e)

test.html

<form method="post" class="text-center" style="color: #757575;" action="">

                <div class="form-row">
                    <div class="col">
                        <!-- First name -->
                        <div class="md-form">
                            <input type="text" name="firstname" value="{{request.form.firstname}}" class="form-control">
                            <label for="materialRegisterFormFirstName">First name</label>
                        </div>
                    </div>
                    <div class="col">
                        <!-- Last name -->
                        <div class="md-form">
                            <input type="text" name="lastname" value="{{request.form.lastname}}" class="form-control">
                            <label for="materialRegisterFormLastName">Last name</label>
                        </div>
                    </div>
                </div>

                <!-- File Upload -->
                <div class="md-form">
                    <input type="file" id="fileupload" class="form-control">


                </div>

                <input class="btn btn-info btn-block" type="submit" value="Submit">
            </form>

该方法将默认发布,并且在加载127.0.0.1:5000/test时响应“错误”。该方法始终是GET

1 个答案:

答案 0 :(得分:0)

<form method="post" class="text-center" style="color: #757575;" action="/test" method="post">

            <div class="form-row">
                <div class="col">
                    <!-- First name -->
                    <div class="md-form">
                        <input type="text" name="firstname" value="{{request.form.firstname}}" class="form-control">
                        <label for="materialRegisterFormFirstName">First name</label>
                    </div>
                </div>
                <div class="col">
                    <!-- Last name -->
                    <div class="md-form">
                        <input type="text" name="lastname" value="{{request.form.lastname}}" class="form-control">
                        <label for="materialRegisterFormLastName">Last name</label>
                    </div>
                </div>
            </div>

            <!-- File Upload -->
            <div class="md-form">
                <input type="file" id="fileupload" class="form-control">


            </div>

            <input class="btn btn-info btn-block" type="submit" value="Submit">
        </form>

通过在操作属性中添加“路线”并定义提交表单的方法,您可以执行所需的目标。