Bootstrap / Flask模式错误的发布请求

时间:2016-04-11 13:50:14

标签: python twitter-bootstrap flask

我正在尝试使用FlaskBoostrap获取模态输入表单的内容。关于SO的类似问题的大多数其他回答都谈到了为form action添加路径,但这并没有解决这个问题。造成这个问题的原因是什么?

init.py

from flask import Flask, render_template, request

app = Flask(__name__)
app.secret_key = "Some secret string here"
@app.route('/getname', methods=["GET", "POST"])
def getname():

    if request.method == "POST":
        print request.form['value of Id']
    return render_template("done.html")

if __name__ == "__main__":
    app.run(debug=True)

html模式形式:

<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
               <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
            </div>
            <div class="modal-body">
                <form method="post" role="form" action="/getname">
                    <div class="form-group">
                        <label for="Name" class="control-label">Name:</label>
                        <input type="text" class="form-control" id="Name">

                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <a href="/getname"><button type="submit" id="btnUpdate" class="btn btn-primary">Submit</button></a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

2 个答案:

答案 0 :(得分:2)

您需要使用name属性而不是id。这不是Flask甚至Python特有的东西,而是HTTP。

from flask import Flask, render_template, request

app = Flask(__name__)
app.secret_key = "Some secret string here"
@app.route('/getname', methods=["GET", "POST"])
def getname():

    if request.method == "POST":
        print request.form['Name']
    return render_template("done.html")

if __name__ == "__main__":
    app.run(debug=True)

<form method="post" role="form" action="/getname" id="form1">
    <div class="form-group">
        <label for="Name" class="control-label">Name:</label>
        <input type="text" class="form-control" id="Name" name="Name">

        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    </div>
</form>
<button type="submit" id="btnUpdate" class="btn btn-primary" form="form1">Submit</button>

答案 1 :(得分:1)

<form method="post" role="form" action="/getname" id="form1">
    <div class="form-group">
        <label for="Name" class="control-label">Name:</label>
        <input type="text" class="form-control" id="Name">

        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    </div>
</form>
<button type="submit" id="btnUpdate" class="btn btn-primary" form="form1">Submit</button>

看来你的按钮错位了,试试这个。