본문 바로가기
PRACTICE/Basic

[Flask] render_template 사용하여 count 출력

by 1005 2020. 9. 3.
 

# 경로 렌더링 render_template

# 꼭 templates 폴더를 만들어 주어야 함.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from flask import Flask, render_template
 
app = Flask(__name__) 
cnt = 0 #전역변수
 
@app.route('/')
def count():
    global cnt #전역변수를 함수 내에서 사용하기 위해 global 붙여줌.
    cnt += 1
    return render_template('count.html',cnt = cnt)
 
if __name__ == '__main__':
    app.run(host='0.0.0.0',debug=True, port='80')
 

 

 

< count.html >

 

1
2
3
4
5
<html>
<body>
    <h1>Visit Count: {{cnt}}</h1>
</body>
</html>

 

 

처음 페이지를 열면 count값이 1 증가한 1값이 출력됨.

 

페이지를 n번 새로고침하면 n값이 출력됨. 사진은 7번 새로고침하여 7이 출력되었음.

 

 

댓글