Flask 초보 입니다. The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

조회수 7044회
from flask import Flask
from flask import url_for,render_template,request
app = Flask(__name__)


@app.route("/hello/", methods=['POST','GET'])
def profile(username=None):
    error = None
    if request.method == 'POST':
        username = request.form['username']
        email = request.form['email']
        if not username and not email:
            return add_profile(request.form)
    else:
        error='Invalid username or email'

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

Flask 책에서 본 코드입니다.

거의 초반 부분인데요 실행해서 /profile/로 넘어가면

Not Found

The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

라는 에러가 뜹니다.

사실 저 코드를 잘 이해하지 못해서 에러를 해결하지도 못하고 꼼짝 못하고 있습니다.

저 코드가 뭐하는 코드이며 어떻게 동작되는지 설명해 주실 수 있으신 마음씨 좋은 개발자분 정보좀 부탁 드립니다.

1 답변

  • @app.route("/hello/", methods=['POST','GET']) route라는 데코레이터를 사용하고 있는데요. 바로 서비스 url을 지정해주는 겁니다.

    profile 이 아니라 route에 선언한 /hello를 호출해야하죠.

    즉 위 코드는 /hello 를 호출하면 profile 라는 함수를 수행하게 되며 메소드는 POST와 GET만 허용하겠다는 겁니다.

    HTTP 1.1의 메소드는 POST와 GET말고 PUT, DELETE, HEAD, OPTIONS, TRACE 가 더 있습니다.

    그런데 더 문제는 상기의 코드를 수행하려면 준비물(입력폼)이 더 필요합니다. 상기의 코드는 입력폼이 있고 그 입력폼에는 username, email 라는 필드가 있어 그 값을 채워서 submit을 할 때 수행(등록)되는 로직입니다.

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)