Flask 시작하기 - HTML 파일 주기(4-4)

Flask 시작하기 - HTML 파일 주기(4-4)

웹개발 공부 4주차 - 4

1. Flask 기초: 기본 폴더구조 - 항상 이렇게 세팅하고 시작

ㅇ Framework의 규칙을 따라야 하고, 정해진 폴더 규칙이 있음

ㅇ 프로젝트 폴더 안에 아래 폴더(Directory)들 생성

- static 폴더 (이미지, css 파일들)

- templates 폴더 (html 파일들)

- app.py 파일

ㅇ templates 폴더 안에 index.html 생성 (역시 기본적으로 많이 쓰이는 명칭 index)

※ 명칭이랑은 저대로 써야 함

ㅇ 에 대충 내용 입력

ㅇ app.py 맨 윗줄에 render_template 추가, return 값에 render_template('index.html') 적고 실행 후 브라우저 확인

ㅇ render_template 라는 Flask에서 정해둔 함수를 이용하면 위에처럼만 써도 자동으로 templates 라는 폴더

안에 있는 'index.html' 이라는 파일을 가져다 표시

ㅇ 사용자가 localhost:5000/ 들어오면 @app.route('/')로 와서 밑의 home() 함수를 실행하고,

render_template() 함수를 통해 templates 폴더 안의 'index.html'을 불러와 return

ㅇ index.html 예제 코드

Document

서버를 만들었다!

- 별거 없고, jQuery 미리 받아놓음

from http://imwithavril.tistory.com/47 by ccl(A) rewrite - 2021-07-19 02:00:45