본문 바로가기
PRACTICE/Basic

[Flask] request.form 사용하여 key, value 값 전달

by 1005 2020. 9. 23.

 

< 폴더 경로 >

 

 

 

< app.py>

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from flask import Flask, render_template, request
app = Flask(__name__)
 
@app.route('/')
def student():
   return render_template('addrbook.html')
 
@app.route('/pbook',methods = ['POST''GET'])
def result():
   if request.method == 'POST':
      val = request.form #addrbook.html에서 name을 통해 submit한 값들을 val 객체로 전달
      return render_template("pbook.html",result = val) #name은 key, name에 저장된 값은 value
 
if __name__ == '__main__':
   app.run(host='0.0.0.0', port = 80, debug = True)

 

 

 

< addrbook.html >

 

1
2
3
4
5
6
7
8
9
10
<html>
   <body>
      <form action = "http://localhost/pbook" method = "POST">
         <p>이름: <input type = "text" name = "이름" /></p>
         <p>위치: <input type = "text" name = "위치" /></p>
         <p>전화번호: <input type = "text" name = "전화번호" /></p>
         <p><input type = "submit" value = "저장"" /></p>
      </form>
   </body>
</html>

 

< pbook.html >

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
<!doctype html>
<html>
   <body>
      <table border = 1>
         {% for key, value in result.items() %}
            <tr>
               <th> {{ key }} </th>
               <td> {{ value }} </td>
            </tr>
         {% endfor %}
      </table>
   </body>
</html>

 

 

 

 

 

 

 

댓글