POST์ GET ๋ชจ๋ ์๋์ ๊ฐ์ ์์๋ก ๋ง๋ค ๊ฒ์ด๋ค.
1. ํด๋ผ์ด์ธํธ์ ์๋ฒ ์ฐ๊ฒฐ ํ์ธํ๊ธฐ
2. ์๋ฒ ๋ง๋ค๊ธฐ
3. ํด๋ผ์ด์ธํธ ๋ง๋ค๊ธฐ
4. ์์ฑ ํ์ธํ๊ธฐ
โถ POST API ๋ง๋ค๊ธฐ
์ด๋ฆ, ์ฃผ์, ํ์ ์ ์ฅํ๊ธฐ(Create → POST)
1. ์์ฒญ ์ ๋ณด: URL= /mars, ์์ฒญ ๋ฐฉ์ = POST
2. ํด๋ผ์ด์ธํธ(ajax) → ์๋ฒ(flask) : name, address, size
3. ์๋ฒ(flask) → ํด๋ผ์ด์ธํธ(ajax) : ๋ฉ์์ง๋ฅผ ๋ณด๋ (์ฃผ๋ฌธ ์๋ฃ)
* ์๋ฒ ๋ง๋ค๊ธฐ
→ name, address, size ์ ๋ณด๋ฅผ ๋ฐ์์ ์ ์ฅ
@app.route("/mars", methods=["POST"])
def web_mars_post():
name_receive = request.form['name_give']
address_receive = request.form['address_give']
size_receive = request.form['size_give']
doc = {
'name': name_receive,
'address': address_receive,
'size': size_receive
}
db.mars.insert_one(doc)
return jsonify({'msg': '์ฃผ๋ฌธ ์๋ฃ'})
* ํด๋ผ์ด์ธํธ ๋ง๋ค๊ธฐ
→ name, address, size ์ ๋ณด ๋ณด๋ด๊ธฐ
function save_order() {
let name = $('#name').val()
let address = $('#address').val()
let size = $('#size').val()
$.ajax({
type: 'POST',
url: '/mars',
data: {name_give: name, address_give: address, size_give: size},
success: function (response) {
alert(response['msg'])
window.location.reload()
}
});
}
โถ GET API ๋ง๋ค๊ธฐ
์ ์ฅ๋ ์ฃผ๋ฌธ์ ํ๋ฉด์ ๋ณด์ฌ์ฃผ๊ธฐ(Read → GET)
1. ์์ฒญ ์ ๋ณด: URL= /mars, ์์ฒญ ๋ฐฉ์ = GET
2. ํด๋ผ์ด์ธํธ(ajax) → ์๋ฒ(flask) : (์์)
3. ์๋ฒ(flask) → ํด๋ผ์ด์ธํธ(ajax) : ์ ์ฒด ์ฃผ๋ฌธ์ ๋ณด๋ด์ฃผ๊ธฐ
* ์๋ฒ ๋ง๋ค๊ธฐ
→ ๋ฐ์์ฌ ๊ฒ ์์ด orders์ ์ฃผ๋ฌธ ์ ๋ณด๋ฅผ ๋ด์์ ๋ด๋ ค์ฃผ๊ธฐ๋ง ํ๋ฉด ๋จ
@app.route("/mars", methods=["GET"])
def web_mars_get():
order_list = list(db.mars.find({}, {'_id': False}))
return jsonify({'orders':order_list})
* ํด๋ผ์ด์ธํธ ๋ง๋ค๊ธฐ
function show_order() {
$.ajax({
type: 'GET',
url: '/mars',
data: {},
success: function (response) {
let rows = response['orders']
for(let i =0; i< rows.length; i++) {
let name = rows[i]['name']
let address = rows[i]['address']
let size = rows[i]['size']
let temp_html = `<tr>
<td>${name}</td>
<td>${address}</td>
<td>${size}</td>
</tr>`
$('#order-box').append(temp_html)
}
}
});
}
์์ฑ๋ ์ ์ฒด์ฝ๋์ด๋ค.
< app.py > _์๋ฒ
from flask import Flask, render_template, request, jsonify
app = Flask(__name__)
from pymongo import MongoClient
client = MongoClient('mongodb+srv://id:pw@cluster0.7n9jh.mongodb.net/Cluster0?retryWrites=true&w=majority')
db = client.dbsparta
//index.html ํ์ผ ๋ถ๋ฌ์ค๊ธฐ
@app.route('/')
def home():
return render_template('index.html')
//POST
@app.route("/mars", methods=["POST"])
def web_mars_post():
name_receive = request.form['name_give']
address_receive = request.form['address_give']
size_receive = request.form['size_give']
doc = {
'name': name_receive,
'address': address_receive,
'size': size_receive
}
db.mars.insert_one(doc)
return jsonify({'msg': '์ฃผ๋ฌธ ์๋ฃ'})
//GET
@app.route("/mars", methods=["GET"])
def web_mars_get():
order_list = list(db.mars.find({}, {'_id': False}))
return jsonify({'orders':order_list})
if __name__ == '__main__':
app.run('0.0.0.0', port=5000, debug=True)
<index.html> _ํด๋ผ์ด์ธํธ
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
crossorigin="anonymous"></script>
<link href="https://fonts.googleapis.com/css2?family=Gowun+Batang:wght@400;700&display=swap" rel="stylesheet">
<title>์ ์ฐฉ์ ๊ณต๋๊ตฌ๋งค</title>
<style>
* {
font-family: 'Gowun Batang', serif;
color: white;
}
body {
background-image: linear-gradient(0deg, rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url('https://cdn.aitimes.com/news/photo/202010/132592_129694_3139.jpg');
background-position: center;
background-size: cover;
}
h1 {
font-weight: bold;
}
.order {
width: 500px;
margin: 60px auto 0px auto;
padding-bottom: 60px;
}
.mybtn {
width: 100%;
}
.order > table {
margin: 40px 0;
font-size: 18px;
}
option {
color: black;
}
</style>
<script>
//GET
$(document).ready(function () {
show_order();
});
function show_order() {
$.ajax({
type: 'GET',
url: '/mars',
data: {},
success: function (response) {
let rows = response['orders']
for(let i =0; i< rows.length; i++) {
let name = rows[i]['name']
let address = rows[i]['address']
let size = rows[i]['size']
let temp_html = `<tr>
<td>${name}</td>
<td>${address}</td>
<td>${size}</td>
</tr>`
$('#order-box').append(temp_html)
}
}
});
}
//POST
function save_order() {
let name = $('#name').val()
let address = $('#address').val()
let size = $('#size').val()
$.ajax({
type: 'POST',
url: '/mars',
data: {name_give: name, address_give: address, size_give: size},
success: function (response) {
alert(response['msg'])
window.location.reload()
}
});
}
</script>
</head>
<body>
<div class="mask"></div>
<div class="order">
<h1>ํ์ฑ์ ๋
์ฌ๋๊ธฐ!</h1>
<h3>๊ฐ๊ฒฉ: ํ ๋น 500์</h3>
<p>
ํ์ฑ์ ๋
์ ์ฌ๋ ์ ์๋ค๊ณ ?<br/>
์์ผ๋ก ๋ฐฑ๋
๊ฐ ์ค์ง ์์ ๊ธฐํ. ํ์ฑ์์ ์ฆ๊ธฐ๋ ๋
ธํ!
</p>
<div class="order-info">
<div class="input-group mb-3">
<span class="input-group-text">์ด๋ฆ</span>
<input id="name" type="text" class="form-control">
</div>
<div class="input-group mb-3">
<span class="input-group-text">์ฃผ์</span>
<input id="address" type="text" class="form-control">
</div>
<div class="input-group mb-3">
<label class="input-group-text" for="size">ํ์</label>
<select class="form-select" id="size">
<option selected>-- ์ฃผ๋ฌธ ํ์ --</option>
<option value="10ํ">10ํ</option>
<option value="20ํ">20ํ</option>
<option value="30ํ">30ํ</option>
<option value="40ํ">40ํ</option>
<option value="50ํ">50ํ</option>
</select>
</div>
<button onclick="save_order()" type="button" class="btn btn-warning mybtn">์ฃผ๋ฌธํ๊ธฐ</button>
</div>
<table class="table">
<thead>
<tr>
<th scope="col">์ด๋ฆ</th>
<th scope="col">์ฃผ์</th>
<th scope="col">ํ์</th>
</tr>
</thead>
<tbody id="order-box">
</tbody>
</table>
</div>
</body>
</html>
'๐ ๊ธฐํ > ์คํ๋ฅดํ์ฝ๋ฉํด๋ฝ_FE' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์น๊ฐ๋ฐ ์ข ํฉ๋ฐ | Flask ์ฌ์ฉํ๊ธฐ (0) | 2021.12.27 |
---|---|
์ง๋๋ฎค์ง ํฌ๋กค๋งํ๊ธฐ (0) | 2021.12.27 |
์น๊ฐ๋ฐ ์ข ํฉ๋ฐ | ์น์คํฌ๋ํ(ํฌ๋กค๋ง) | DB | mongoDB (0) | 2021.12.26 |
์น๊ฐ๋ฐ ์ข ํฉ๋ฐ | jQuery | Ajax (0) | 2021.12.23 |
ํฌ๋ช ๋ก ๋ง๋ค๊ธฐ (0) | 2021.12.22 |