Coding/Python
[Python & Flask] FORM 태그로 데이터 보내기 (POST, GET)
포스트it
2022. 4. 18. 10:32
728x90
반응형

[Python & Flask] FORM 태그로 데이터 보내기 (POST, GET)
app.py
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/form', methods = ['POST', 'GET'])
def form():
if request.method == 'POST':
result = request.form
print(result)
return render_template("form.html", result=result)
if __name__ == '__main__':
app.run(debug = True)
render_template, request 를 import 해주신 후
폴더 구조를 이렇게 맞춰주세요. (templates 폴더를 생성해서 안에 넣어주셔 합니다.)

index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form action="http://127.0.0.1:5000/form" method="POST">
<p>Id <input type="text" name="Id"/></p>
<p>Password <input type="password" name="Password"/></p>
<p><input type="submit" value="submit"/></p>
</form>
</body>
</html>
form.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<table>
{% for key, value in result.items() %}
<tr>
<th> {{ key }} </th>
<td> {{ value }} </td>
</tr>
{% endfor %}
</table>
</body>
</html>
이렇게 작성하시고 flask 실행하시면 끝 !
결과값


728x90
반응형