import datetime
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/simple_page')
def get_simple_page():
dt = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
return render_template('simple_page.html', current_time=dt)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=31337)
템플릿
템플릿 렌더링 기호
render_temlate() : HTML 템플릿에 존재하는 특수 기호를 규칙에 맞춰 렌더링함
변수의 이름을 이중 중괄호로 감싼 {{변수 이름}} 형태를 HTML 템플릿 내용에 작성하면 변수 이름 에 해당하는 데이터를 HTML 문서에 삽입할 수 있음
이러한 것이 가능한 건 jinja2라는 템플릿 엔진을 render_template 함수가 내부적으로 사용하고 있기 때문임.
<!DOCTYPE html>
<html>
<head>
<title>My Simple Web Page</title>
</head>
<body>
<h1>Explore the World of HTML!</h1>
<p>Greetings from the Web!</p>
<h2>{{ current_time }}</h2>
</body>
</html>