.py
render_template('html이름.html', 전달변수명=전달변수명)
.html
- 꼭 html templates 폴더 안에 넣아야 한다!
{{전달받은파라미터}}
예시
my_flask.py
from flask import Flask, request, render_template, redirect
app = Flask(__name__)
@app.route('/forw')
def forw():
a = "홍길동"
b = ["전우치", "장화홍련"]
return render_template('forw.html', a=a, b=b)
if __name__ == '__main__':
app.run(debug=True, port=80, host='0.0.0.0')
forw.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
forw.html <br>
a= {{a}} <br>
b= {{b}} <br>
<hr>
{% for i in b %}
b={{i}}<br>
{%endfor%}
</body>
</html>
my_flask.py
from flask import Flask, request, render_template, redirect
app = Flask(__name__)
@app.route('/emp')
def emp():
mylist = [
{'e_id': '1', 'e_name': '1', 'gen': '1', 'addr': '1'},
{'e_id': '2', 'e_name': '2', 'gen': '2', 'addr': '2'},
{'e_id': '3', 'e_name': '3', 'gen': '3', 'addr': '3'}
]
return render_template('emp.html', mylist=mylist)
if __name__ == '__main__':
app.run(debug=True, port=80, host='0.0.0.0')
emp.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<style>
table{ border-collapse : collapse; } /*이중선 제거*/
th,td{
width: 100px;
height: 50px;
text-align: center;
border: 1px solid #000;
vertical-align: top; /* 위 */
vertical-align: bottom; /* 아래 */
vertical-align: middle; /* 가운데 */
}
</style>
<body>
EMP HTML <br>
<p>for문 안할경우 :{{mylist}}</p>
<table border='1'>
<tr>
<th>아이디</th>
<th>이름</th>
<th>성별</th>
<th>주소</th>
</tr>
{% for i in mylist %}
<tr>
<a href="#">
<td>{{i.e_id}}</td>
</a>
<td>{{i.e_name}}</td>
<td>{{i.gen}}</td>
<td>{{i.addr}}</td>
</tr>
{%endfor%}
</table>
</body>
</html>
'Python > flask' 카테고리의 다른 글
axios (0) | 2024.04.10 |
---|---|
dao 이용한 select,insert,update,delete문 (0) | 2024.04.09 |
flask Insert,Update, Delete 문 (0) | 2024.04.03 |
flask get방식, post 방식 파라미터 넣기 (0) | 2024.04.03 |
flask 서버 실행 (0) | 2024.04.03 |