Flask Request Object II - Form

Form:

example:

vi templates/record.html

<!doctype html>
<html>
  <body>
    <table border = 1>
      {% for key, value in input.iteritems() %}

        <tr>
          <th> {{ key }} </th>
          <td> {{ value }} </td>
        </tr>

      {% endfor %}
    </table>

  </body>
</html>
vi templates/friend.html
<html>
  <body>

    <form action = "http://<ip/url>:5000/record" method = "POST">
       <p>Name <input type = "text" name = "name" /></p>
       <p>Age <input type = "text" name = "age" /></p>
       <p>Higher <input type = "text" name = "higher" /></p>
       <p>Weight <input type ="text" name = "weight" /></p>
       <p><input type = "submit" value = "submit" /></p>
    </form>

  </body>
</html>
vi friend_rec.py
from flask import Flask, render_template, request
ap = Flask(__name__)

@ap.route('/')
def friend():
  return render_template('friend.html')

@ap.route('/record',methods = ['POST', 'GET'])
def record():
  if request.method == 'POST':
    input = request.form
    return render_template("record.html",input = input)

if __name__ == '__main__':
  ap.run(host= '0.0.0.0')
python friend_rec.py

#open
http://<ip/url>:5000



Leave comments

authimage

Copyright(c) 2017 - PythonBlogs.com
By using this website, you signify your acceptance of Terms and Conditions and Privacy Policy
All rights reserved