2016-04-11 2 views
0

Я пытаюсь получить содержимое модальной формы ввода с Flask и Boostrap. Большинство других ответов на подобные вопросы на SO говорят о добавлении пути для form action, но это не решение этого. Что вызывает здесь проблему?Bootstrap/Flask modal Плохой запрос для сообщения

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> 

ответ

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> 

Кажется, вы mislocated своей кнопки, попробуйте это.

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> 
+0

Спасибо !! Я также пропустил поле «имя», и ваше решение решило проблему. – user1501382

Смежные вопросы