Flask

HEUKWU·2023년 1월 9일
0

파이썬 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파일을 정상적으로 렌더링하는 것을 볼 수 있다.

0개의 댓글

관련 채용 정보