๐ฉ๐ป ๋ฐฑ์๋(Back-End)/Node js
๋ค์ ๋์ .. Flask ์๋ฒ์์ ํด๋ผ์ด์ธํธ๋ก ๋ฐ์ดํฐ ๋ณด๋ด๊ธฐ (2)
์ง์ง์ํ์นด
2022. 11. 15. 14:47
728x90
๋ฐ์ํ
221115 ์์ฑ
<๋ณธ ๋ธ๋ก๊ทธ๋ wikidocs ์ ๋ธ๋ก๊ทธ๋ฅผ ์ฐธ๊ณ ํด์ ๊ณต๋ถํ๋ฉฐ ์์ฑํ์์ต๋๋ค :-)>
01-6 ํด๋ผ์ด์ธํธ์์ ์๋ฒ๋ก ๋ฐ์ดํฐ ๋ณด๋ด๊ธฐ1
# html์ formํ๊ทธ๋ฅผ ์์ฑ - post์ ๊ฒฝ์ฐ name์์ฑ์ ์๋ฒ์์ ์ฌ์ฉํ ๋ณ์๋ช ๋๋ ํค ๊ฐ์ด ๋ฉ๋๋ค. ```html Title …
wikidocs.net
๐ ๊ธฐ๋ณธ์ ์ธ ์นํ์ด์ง ๊ตฌ์ถ
- main.js
- ํ์ฌ ์์..
- style.css
h1{
color : blue;
}
- index.html
<!-- ํ์ํ html ํ์ผ์ ๋ชจ์๋ ์์ -->
<!--index.html-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>
<h1>๋๋ฌธ ํ์ด์ง</h1>
์ด๋ฆ : {{user}}
๋ ๋ฒจ : {{data.level}}
ํฌ์ธํธ : {{data.point}}
๊ฒฝํ์น : {{data.exp}}
<form action="/" method="get">
๋ค์ ์ด๋ฆ : <input type="text" name="user">
<input type="submit" value="์ ์กํ๊ธฐ">
</form>
</body>
</html>
- app.py
##### ์คํ์ ์๋ฒ๊ฐ ๊ฐ๋๋ ํ์ด์ฌ ํ์ผ #####
# /app.py
from flask import Flask, render_template, request
#Flask ๊ฐ์ฒด ์ธ์คํด์ค ์์ฑ
app = Flask(__name__)
@app.route('/',methods=('GET', 'POST')) # ์ ์ํ๋ url
def index():
if request.method == "POST":
# user=request.form['user'] # ์ ๋ฌ๋ฐ์ name์ด user์ธ ๋ฐ์ดํฐ
print(request.form.get('user')) # ์์ ํ๊ฒ ๊ฐ์ ธ์ค๋ ค๋ฉด get
user = request.form.get('user')
data = {'level': 60, 'point': 360, 'exp': 45000}
return render_template('index.html', user=user, data=data)
elif request.method == "GET":
user = "๋ฐ์"
data = {'level': 60, 'point': 360, 'exp': 45000}
return render_template('index.html', user=user, data=data)
if __name__=="__main__":
app.run(debug=True)
# host ๋ฑ์ ์ง์ ์ง์ ํ๊ณ ์ถ๋ค๋ฉด
# app.run(host="127.0.0.1", port="5000", debug=True)
๐ ๊ฒฐ๊ณผ
728x90
๋ฐ์ํ