파이썬 flask를 이용해 서버를 열었는데 파일을 하나밖에 열지 못한다.
@app.route('/')
def home():
return render_template('index.html')
render_template
함수를 이용해 index.html
파일을 여는 것이다. html파일에서 특정 버튼을 클릭하면 다른 페이지로 이동하는 기능을 구현하고 싶어
<li><a href="#" onclick="location.href='index2.html">순위</a></li>
이처럼 html문서에 onclick
을 추가하고 index2.html
파일로 이동하도록 작성했다.
그리고 나서 버튼을 눌러보니 404가 뜬다.
index.html
파일만 렌더링하도록 설정돼 있으니 다른 파일은 읽지 못하는 것이다.
<li><a href="#" onclick="location.href='/rank">순위</a></li>
이처럼 href
안에 /와 원하는 주소를 입력한다.
그리고 파이썬 파일로 가서
@app.route('/rank')
def rank():
return render_template('index3.html')
app.route
함수안에 입력한 주소를 넣어주고,
rank
라는 함수를 생성해 rnder_template
함수가 index2.html
파일을 렌더링하도록 설정해준다. 이제 버튼을 클릭하면 /rank
주소로 연결되고 index2.html
파일을 정상적으로 렌더링하는 것을 볼 수 있다.