728x90
반응형
우선 다음과 같이 파일이 담겨 있다.
[application.py]
# -*- coding: utf-8 -*-
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/')
def root():
return '''
<a href="/hello">hello</a>
<input type="button" value="hello" onclick="location.href='/hello/name'"/>
'''
@app.route('/hello')
@app.route('/hello/<name>')
def hello(name=None):
return render_template('hello.html', name=name)
@app.route('/test', methods=['POST'])
def test():
return render_template('test.html', test=request.form['command'])
if __name__ == '__main__':
app.run()
[hello.html]
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
{% if name %}
<h1>Hello, {{name}}!</h1>
{% else %}
<h1>Hello, Flask!</h1>
{% endif %}
<form action="/test" method="post">
<input type="hidden" name="command" value="value test"/>
<input type="submit" value="test"/>
</form>
</body>
</html>
[test.html]
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<!--css이어주기-->
<link rel="stylesheet" href="{{url_for('static',filename='test.css')}}"/>
</head>
<body>
<h1>Hello, {{test}}!</h1>
<a href="/">root</a>
</body>
</html>
[test.css]
h1{
color: red;
}
[결과화면]
<a태그 hello 클릭>
<a태그 hello - test 클릭>
728x90
반응형
'Language > Python' 카테고리의 다른 글
[코딩도장]06.변수와 입력 (0) | 2022.03.12 |
---|---|
[코딩도장]03~05.기본문법 (0) | 2022.03.12 |
[python]flask01 (0) | 2020.10.13 |
[python]crawling_크롤링_starbucks (0) | 2020.10.13 |
[python]crawling_크롤링_인스타 (0) | 2020.10.12 |