TIL/Python

[Python][Flask] 플라스크가 뭘까? -2. 디버깅

탱! 2022. 5. 18. 16:15

2. 실행시켰는데 서버가 생기지 않을 때

1) 플라스크의 버전을 확인해보자 

2) 파일이름을 잘 입력했는 지 확인하자

3. 디버그 모드

실행 중에도 만일 코드가 변하면 자동적으로 리업로드하는데 이때 발생한는 디버그를 웹에 나타내준다.

실행중

위와 같이 실행중에 코드를 변경해보면

@app.route('/name')
def hello_world2():
    return "<h1>Hellow, World</h1><p>What is your first name?</p>"

처럼 변경되어서 나오는데 만일 오류가 생긴다면?

 

우선 변수설정을 해보자

@app.route('/<name>')
def hello_world2(name):
    return f"<h1>Hellow, World</h1><p>What is your favorite {name}?</p>"

사용하게될 변수를 <>안에 넣어준다.

여기서 오류를 만들어보자

@app.route('/<name>')
def hello_world2(name):
    return f"<h1>Hellow, World</h1><p>What is your favorite {name+34}?</p>"

위와 같은 화면이 뜬다.

만일 무엇때문인지 알고 싶다면 아래와 같은 코드로 디버깅모드를 키자

if __name__ == "__main__":
    app.run(debug=True)

str은 str 끼리만 사용할 수 있다는 타입에러가 웹에 뜬다.

콘솔에는

시큐리티 핀코드를 제공해준다. 이걸로 오류를 확인할 수 있다.

코드핀을 입력하고 콘솔창을 사용할 수 있다.

 

4. 변수 규칙

변수변환 타입에는 다섯가지가 있다.

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

int 와 path 두 가지로 사용해보자

@app.route('/post/<int:post_id>/<path:number>')
def show_post(post_id,number):
    # show the post with the given id, the id is an integer
    return f'<h1>Your Post id is {post_id}</h1><h2>and the number is {number}</h2>'