오답노트
[flask] html 로부터 request 받기 본문
app.py
from flask import Flask, request, render_template
app = Flask(__name__)
@app.route("/")
def home():
return render_template("home.html")
@app.route("/login", methods=["POST"]) # request 받을 method 지정 (GET, POST 등)
def login_method():
if request.method == "POST": # 요청 받은 method 가 POST 인 경우
name = request.form["user_id"]
passwd = request.form(["user_pw"]) # input text 의 name 태그의 값을 받아온다
return print(f"the user is {name}, {passwd}")
if __name__ == "__main__":
app.run(debug=True, port=8080) # debug 모드 활성화, 포트넘버 8080 으로 지정 후 서버 실행
'''
해당 코드를 실행시키면 TypeError 가 뜨는데, 이는 추후 해결하도록 하겠다.
중요한 부분은 request.form 을 통해 name 태그 내의 값을 받아오는 것.
'''
home.html
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>HomePage</title>
</head>
<body>
<h1>This page is home page</h1>
<p>Welcome this web site</p>
<form action="http://localhost:8080/login" method="post">
<input type="text" name="user_id" placeholder="ID"><br>
<input type="password" name="user_pw" placeholder="PASSWORD"><br>
<input type="submit"><br>
<a href="http://localhost:8080/register">[Register]</a><br>
<a href="http://localhost:8080/manager">[Manager]</a>
</form>
</body>
</html>
'파이썬' 카테고리의 다른 글
[python] 영상에서 음성 추출하기 (0) | 2022.02.07 |
---|---|
[python] UnboundLocalError: local variable 'py' referenced before assignment (0) | 2022.02.07 |
[python] DB 연결 (0) | 2022.01.27 |
[python] 클래스 오버라이딩 (Class overriding) (0) | 2022.01.25 |
[python] json, dict (0) | 2022.01.24 |