728x90
플라스크로 멀티페이지 만들기
파이참 설정 단축기 : ctrl+alt+s
<패키지 설치>
- 서버구축
flask
pymongo
certifi(나는 필요)
- 오픈API에다 요청보내야하니까
requests
---app.py
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def main():
return render_template("index.html")
@app.route('/detail')
def detail():
return render_template("detail.html")
if __name__ == '__main__':
app.run('0.0.0.0', port=5000, debug=True)
---index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>메인페이지</h1>
<a href="/detail">상세페이지</a>
</body>
</html>
-----detail.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
function to_main() {
window.location.href="/"
}
</script>
</head>
<body>
<h1>상세페이지</h1>
<button onclick="to_main()">메인으로 돌아가기</button>
</body>
</html>
한줄짜리 function은 그냥 onclick 안에 넣어도 OK. 큰따옴표 두개가 겹치니까 하나는 작은 따옴표로 바꿔줌.
<button onclick="window.location.href='/'">
jinja2 템플릿 언어로 페이지에 값 끼워넣기
---app.py
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def main():
myname = "Sparta"
return render_template("index.html", name=myname)
@app.route('/detail')
def detail():
return render_template("detail.html")
if __name__ == '__main__':
app.run('0.0.0.0', port=5000, debug=True)
메인페이지 띄울 때 name도 같이 보내줌.
---index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>메인페이지</h1>
<a href="/detail">상세페이지</a>
<h3>{{name}}아 안녕!</h3>
</body>
</html>
jinja템플릿 언어로는 {{ }}로 끼워넣을 것 표시.
ajax쓸 때 꼭 이 줄이 들어가야 함!!! 템플릿 옮기는 과정에서 빠뜨려서 오류났었음..ㅎㅎ
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
설정-언어및프레임워크-템플릿언어에서
none을 jinja2로 바꿔줘
<li>{{rows[0]["MSRSTE_NM"]}}: {{rows[0]["IDEX_MVL"]|int}}</li>
|이 특수문자 이름은 파이프!
|int(=integer) 정수로 나타내줘
{% for row in rows %}
{% set gu_name = row["MSRSTE_NM"] %}
{% set gu_mise = row["IDEX_MVL"] %}
{% if gu_mise>=60 %}
<li>{{gu_name}}: {{gu_mise|int}}</li>
{% endif %}
{% endfor %}
파이썬은 들여쓰기로 시작과 끝 표현해주지만
html에서는 다른 의미이기 때문에 end를 꼭 따로 표시해줘야한다.
검색창에 http://localhost:5000/detail?word_give=hi
물음표 이하를 서버에 겟 요청 보내는 것.
word_receive = request.args.get("word_give")
print(word_receive)
겟요청보낸 파라미터를 request.args.get써서 받을 수 있다.
request(requests랑 다름)는 flask에 자체적으로 있는 함수라서
위에 추가(아래에 requests는 다른 기능 때문에 미리 들어가있던 것)
from flask import Flask, render_template, request
import requests
이렇게 물음표 쓰는 방식말고 주소자체로 받는 방법도 있다.
http://localhost:5000/detail/spatra
@app.route('/detail/<keyword>')
def detail(keyword):
r = requests.get('http://openapi.seoul.go.kr:8088/6d4d776b466c656533356a4b4b5872/json/RealtimeCityAir/1/99')
response = r.json()
rows = response['RealtimeCityAir']['row']
word_receive = request.args.get("word_give")
print(word_receive)
return render_template("detail.html", rows=rows, word = keyword)
@app.route('/detail/<keyword>')
return render_template("detail.html", rows=rows, word = keyword)
'항해99 > 웹개발종합반PLUS' 카테고리의 다른 글
웹개발종합반PLUS 4주차-스위터 완성본 (0) | 2022.09.22 |
---|---|
웹개발종합반PLUS 2주차 -(3) (0) | 2022.09.20 |
웹개발종합반PLUS 2주차 -(2) (0) | 2022.09.20 |
웹개발종합반PLUS 4주차 (1) | 2022.09.19 |
웹개발종합반PLUS 1주차 (0) | 2022.09.19 |