๋ฐ์ํ
์ฌ์ฉ ์คํ : HTML, CSS, Javascript, bootstrap, Ajax, Flask, MongoDB
Post ๋ฐฉ์ ์ฐ์ต ( ๊ธฐ๋กํ๋ ๋ฐฉ๋ฒ)
- API ๋ฅผ ๋ง๋ค๊ณ ์ฌ์ฉํด๋ณด์
- ๋ฒํท๋ฆฌ์คํธ ๊ธฐ๋ก API ( CREATE ์ POST ๋ฐฉ์์ผ๋ก)
- ์์ฒญ์ ๋ณด๋ URL = /bucket ์ด๋ฉฐ
- ์์ฒญ๋ฐฉ์์ POST ๋ฐฉ์์ผ๋ก
- ํด๋ผ์ด์ธํธ (ajax) ์์ ์๋ฒ(flask) ๋ก ๋ณด๋ด๋ฉฐ bucket ์ ๋ณด๋ด๋ ค๊ณ ํ๋ค
- templates ์์ ์๋ index.html ์ด ํ๋ก ํธ์๋ ๋ถ๋ถ์ด๋ฏ๋ก ํต์ ํด๋ผ์ด์ธํธ๋ก ์นญํจ
- bucket์ ์ฌ์ฉ์๋ก๋ถํฐ ์ธํ ํผ์์ ๋ฐ์ ๋ฒํท๋ฆฌ์คํธ ํ ์คํธ ๋ฐ์ดํฐ์ด๋ฉฐ
- ๊ธฐ๋กํ๊ธฐ ๋ฒํผ์ ํด๋ฆญํ๊ฒ ๋๋ฉด ํด๋น ๋ด์ฉ์ ์๋ฒ์ ๋ณด๋ด๊ฒ ๋จ
- ์ฐธ๊ณ ๋ก ajax๋ ๊ฐ๋จํ ๋งํ๋ฉด ํ์ด์ง ์ ์ฒด๋ฅผ ๋ฆฌํ๋ ์ ํ์ง ์๊ณ ์๋ ์ํํ ์ ์๋ ๋น๋๊ธฐ ํต์ ์ด ๊ฐ๋ฅํ๋ฐ,
- ๋น๋๊ธฐ ์๋ฐ์คํฌ๋ฆฝํธ์ XML ์ ๋งํ๋๊ฒ์ผ๋ก
- ์๋ฒ์ ํต์ ํ๊ธฐ ์ํด XMLHttpRequest ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ฉฐ
- ์ด๋ฌํ ๋น๋๊ธฐ์ฑ์ ํตํด ์ฌ์ฉ์์ Event ๊ฐ ์์ผ๋ฉด ์ ์ฒด ํ์ด์ง๊ฐ ์๋ ์ผ๋ถ๋ถ๋ง์ ์ ๋ฐ์ดํธ ํ ์ ์๊ฒ ํด์ค
- ๋จ ์๋ฒ์์ ํ ๊ฐ์ง ์ํํด์ผํ๋ ๊ฒ์ ๋ฒํท๋ฆฌ์คํธ์ ์ด๋ ํ ํํธ๊ฐ ์ด ์ผ์ ์ํํด์ผ ํ๋์ง ์ ํด์ค์ผ ํ๋ ๊ฒ์ธ๋ฐ, ๋ฐ๋ก ๋ฒํธ๋ฅผ ํจ๊ป ๋ฃ์ด์ฃผ๋ ์์ ์ด ํ์ํ๋ค
GET ๋ฐฉ์
- API ๋ง๋ค๊ณ ์ฌ์ฉํ๊ธฐ ( ๋ฒํท๋ฆฌ์คํธ ์กฐํ ) Read ๋ฐฉ์์ผ๋ก Get์ ์ด์ฉํ ๊ฒ
- ์์ฒญ์ ๋ณด URL์ /bucket ์ผ๋ก ํ๊ณ
- ์์ฒญ ๋ฐฉ์์ Get ๋ฐฉ์
- ๊ทธ๋ฆฌ๊ณ ํด๋ผ์ด์ธํธ(ajax)๋ ์๋ฒ(flask)๋ก ํ๋๋ฐ, ์ด๋ฏธ ๋ฐ์ดํฐ๋ DB์ ์ ์ฅ๋์ด ์์(post ๋ฐฉ์)
- ์๋ฒ(flask) ์์ ํด๋ผ์ด์ธํธ(ajax)๋ก ์ ์ฒด ๋ฒํท๋ฆฌ์คํธ๋ฅผ ๋ณด์ฌ์ฃผ์
์ฌ์ฉํ ์ฝ๋
templates > 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+Dodum&display=swap" rel="stylesheet">
<title>์ธ์ ๋ฒํท๋ฆฌ์คํธ</title>
<style>
* {
font-family: 'Gowun Dodum', sans-serif;
}
.mypic {
width: 100%;
height: 200px;
background-image: linear-gradient(0deg, rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url('https://images.unsplash.com/photo-1601024445121-e5b82f020549?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1189&q=80');
background-position: center;
background-size: cover;
color: white;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.mypic > h1 {
font-size: 30px;
}
.mybox {
width: 95%;
max-width: 700px;
padding: 20px;
box-shadow: 0px 0px 10px 0px lightblue;
margin: 20px auto;
}
.mybucket {
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
}
.mybucket > input {
width: 70%;
}
.mybox > li {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
margin-bottom: 10px;
min-height: 48px;
}
.mybox > li > h2 {
max-width: 75%;
font-size: 20px;
font-weight: 500;
margin-right: auto;
margin-bottom: 0px;
}
.mybox > li > h2.done {
text-decoration: line-through
}
</style>
<script>
$(document).ready(function () {
show_bucket();
});
function show_bucket() {
$.ajax({
type: "GET",
url: "/bucket",
data: {},
success: function (response) {
let rows = response['buckets']
for (let i = 0; i < rows.length; i++){
let bucket = rows[i]['bucket']
let num = rows[i]['num']
let done = rows[i]['done']
let temp_html = ``
if (done==0) {
temp_html= ` <li>
<h2>โ
${bucket}</h2>
<button onclick="done_bucket(${num})" type="button" class="btn btn-outline-primary">์๋ฃ!</button>
</li>`
} else {
temp_html = ` <li>
<h2 class="done">โ
${bucket}</h2>
</li>`
}
$('#bucket-list').append(temp_html)
}
console.log(response['buckets'])
}
});
}
function save_bucket() {
let bucket = $('#bucket').val()
$.ajax({
type: "POST",
url: "/bucket",
data: {bucket_give: bucket},
success: function (response) {
alert(response["msg"])
window.location.reload()
}
});
}
function done_bucket(num) {
$.ajax({
type: "POST",
url: "/bucket/done",
data: {num_give: num},
success: function (response) {
alert(response["msg"])
window.location.reload()
}
});
}
</script>
</head>
<body>
<div class="mypic">
<h1>๋์ ๋ฒํท๋ฆฌ์คํธ</h1>
</div>
<div class="mybox">
<div class="mybucket">
<input id="bucket" class="form-control" type="text" placeholder="์ด๋ฃจ๊ณ ์ถ์ ๊ฒ์ ์
๋ ฅํ์ธ์">
<button onclick="save_bucket()" type="button" class="btn btn-outline-primary">๊ธฐ๋กํ๊ธฐ</button>
</div>
</div>
<div class="mybox" id="bucket-list">
</div>
</body>
</html>
์ฌ์ฉํ ์ฝ๋
์๋ฒ(flask) ๋ถ๋ถ
app.py
from flask import Flask, render_template, request, jsonify
app = Flask(__name__)
from pymongo import MongoClient
client = MongoClient('mongodb+srv://test:test@cluster0.wpvuwzi.mongodb.net/Cluster0?retryWrites=true&w=majority')
db = client.dbsparta
@app.route('/')
def home():
return render_template('index.html')
@app.route("/bucket", methods=["POST"])
def bucket_post():
bucket_receive = request.form['bucket_give']
print(bucket_receive)
bucket_list = list(db.bucket.find({}, {'_id':False}))
count = len(bucket_list) + 1
doc = {
'num': count,
'bucket': bucket_receive,
'done': 0,
}
db.bucket.insert_one(doc)
return jsonify({'msg': 'POST(๊ธฐ๋ก) ์ฐ๊ฒฐ ์๋ฃ!'})
@app.route("/bucket/done", methods=["POST"])
def bucket_done():
num_receive = request.form['num_give']
print(num_receive)
db.bucket.update_one({'num': int(num_receive)}, {'$set': {'done': 1}})
return jsonify({'msg': 'bucket ์๋ฃ'})
@app.route("/bucket", methods=["GET"])
def bucket_get():
bucket_list = list(db.bucket.find({}, {'_id': False}))
return jsonify({'buckets': bucket_list})
if __name__ == '__main__':
app.run('0.0.0.0', port=5000, debug=True)
๊ฒฐ๊ณผํ๋ฉด
๋ฐ์ํ
'๐๏ธ์ํํธ์จ์ด > ๐JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[AWS + Filezila] ์น์๋น์ค ๋ฐฐํฌ ์ํ ์์ (2) | 2022.09.23 |
---|---|
[์น์๋น์ค ๋ฐฐํฌ] AWS EC2 + Filezila ์ด์ฉํ๊ธฐ (1) | 2022.09.23 |
[ajax] post, get ๋ฐฉ์ ์ด์ฉํ ์น์ฌ์ดํธ ๋ง๋ค๊ธฐ (0) | 2022.09.22 |
[html + css + js] ๊ณต๊ณต๊ธฐ์จ API ํ์ฉ - ajax (0) | 2022.09.20 |
[Vue.js] ๋ฌธ๋ฒ ๊ธฐ์ด 2ํธ(props, v-bind, v-on, router, axios) (0) | 2022.09.18 |