티스토리 뷰

728x90

이전 글에서 Request 를 살펴 봤는데 Flask 에서 URL요청시 변수를 전달 할 수 있습니다.

<variable_name>으로 URL에 표시하고 app.route 와 연결된 함수의 파라미터(인수)로 전달이 됩니다.

타입을 지정 해야 한다면 <convert:variable_name> 으로 표시를 하면 됩니다.

convert 에 들어갈 타입은 아래와 같습니다.

string (default) accepts any text without a slash
int accepts positive integers
float accepts positive floating point values
path like string but also accepts slashes
uuid accepts UUID strings

 

아래는 예제 입니다.

/hello/이름 이라고 요청하면 웹브라우저에 hello 이름이 출력이 되게 코딩

/hello/<name>

@app.route('/hello/<name>')
def hello(name):
    return "hello {}".format(name)

/input/숫자를 요청 하면 app.route함수 안에 정의된 숫자에 맞는 값을 웹브라우저에 출력이 되게 코딩

/input/<int:num>

@app.route('/input/<int:num>')
def input(num):
    name = ''
    if num == 1:
        name = '도라에몽'
    elif num == 2:
        name = '진구'
    elif num == 3:
        name = '퉁퉁이'
    return "hello {}".format(name)

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함