-
[Python] url_for 사용해서 동적 url 만들기 feat.parameterPython 2021. 11. 10. 19:11
Dynamic URL 이란?
동적 URL이란 웹페이지의 내용이, 서버가 제공하는 매개변수에 따라 바뀌는 URL이다.
예)
localhost:8000/1
=> id = 1
localhost:8000/2
=> id = 2
<!-- 클라이언트 사이드 코드 --> <a href="{{ url_for('comments', id=post['objectID'])}}">{{post["title"]}}</a> <!-- url_for('여기는 라우트에서 받을 함수명'), 여기는 넘겨줄 매개변수의 키 --> <!-- 서버에서 넘겨받은 post라는 dict안에서 key = objectID에 해당하는 value가 매개변수 id에 할당된다 -->
# 서버 사이드 코드 @app.route("/<id>") def comments(id): print(id)
'Python' 카테고리의 다른 글
[Python ] Error: That port is already in use. (0) 2021.11.20 [Python] '1.1k'를 1100으로 바꿔주는 함수 (0) 2021.11.12 [Python] flask의 autoescape 사용하지 않기 (0) 2021.11.10 [Python] Flask, render_template 초기 설정 (0) 2021.11.09 [Python] black : Code Formatter (0) 2021.11.09