(4์ฃผ์ฐจ๋ ์ด๋ฐํ๊ฒ ๋ค์ด์ ,, ์ ๋ฆฌ๊ฐ ์ ์ ๋์ด ์์ ์ฃผ์!)
4์ฃผ์ฐจ ์์
: ์๋ฒ ๋ง๋ค๊ธฐ
์ฝ๋ ๊ด๋ฆฌ ์ํด ํด๋๊ตฌ์กฐ ๋ง๋ค์์
flask ํ๋ ์์ํฌ : ์๋ฒ๋ฅผ ๊ตฌ๋์์ผ์ฃผ๋ ํธํ ์ฝ๋ ๋ชจ์! ๋ณต์กํ ์ผ๋ค ์ฝ๊ฒ ์ธ ์ ์์
flask ์์์ฝ๋
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return 'This is Home!'
if __name__ == '__main__':
app.run('0.0.0.0',port=5000,debug=True)
flask ํ๋ ์์ํฌ ์ฌ์ฉ
๊ธฐ๋ณธ ํด๋ ๊ตฌ์กฐ
- GET ๋ฐฉ์ -> ๋ฐ์ดํฐ ์กฐํํ ๋
- POST ๋ฐฉ์ -> ๋ฐ์ดํฐ ์์ฑ, ๋ณ๊ฒฝ, ์ญ์ ํ ๋
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
4-4 ๊ฐ์๋ถ๋ถ ์ด๋ฐ ๋ค์ ๋ณด์ถฉํ๊ธฐ
์ด๋ ค์์ ๋ค์ ์์ 4๊ฐ๊ฐ ์ค๋น ๋์ด ์์!!!
๊ตฌํํด์ผ ํ๋ ๊ฒ 1. ์ฃผ๋ฌธํ๊ธฐ (POST) 2. ์ฃผ๋ฌธ ๋ณด์ฌ์ฃผ๊ธฐ(GET)
๐ API ๋ง๋ค๊ณ ์ฌ์ฉํ๊ธฐ! - ์ด๋ฆ, ์ฃผ์, ํ์ ์ ์ฅ (Creat -> POST)
1. ์์ฒญ ์ ๋ณด : URL=/mars
, ์์ฒญ ๋ฐฉ์ =POST
2. ํด๋ผ(ajax) โ ์๋ฒ(flask) :name
,address
,size
3. ์๋ฒ(flask) โ ํด๋ผ(ajax) : ๋ฉ์์ง๋ฅผ ๋ณด๋ (์ฃผ๋ฌธ ์๋ฃ!)
(1) ์ฐ๊ฒฐํ์ธํ๊ธฐ
(2) ์๋ฒ ๋ง๋ค๊ธฐ (app.py)
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': '์ฃผ๋ฌธ ์๋ฃ!'})
(3) ํด๋ผ์ด์ธํธ ๋ง๋ค๊ธฐ (index.html)
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()
}
});
}
(4) DB์ ์ ๋ค์ด๊ฐ๋์ง ํ์ธํ๊ธฐ
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>
//๋ก๋ฉ ๋์๋ง์ shoe_order๋ฅผ ๋ถ๋ฅธ๋ค
$(document).ready(function () {
show_order();
});
//show order๋ GET๋ฐฉ์, /mars๊ฐ url
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)
}
}
});
}
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>
app.py
from flask import Flask, render_template, request, jsonify
app = Flask(__name__)
from pymongo import MongoClient
client = MongoClient('mongodb+srv://minjeong:rPfksakfdl1!@cluster0.mtbw8.mongodb.net/myFirstDatabase?retryWrites=true&w=majority')
db = client.dbsparta
@app.route('/')
def home():
return render_template('index.html')
@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': '์ฃผ๋ฌธ ์๋ฃ!'})
@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)
์ฌ๊ธฐ๊น์ง๊ฐ 7๊ฐ์ด์๋๋ฐ ๊ณ์ ๋งํ๋ค. 1๋ฒ์งธ ๋ฌธ์ ๋ ์ง์์ ๋ฃ๋ค๊ฐ ์นดํ์์ ๋ฃ๋๋ฐ LAN ์ฐ๊ฒฐ์ด ์ณ์ง ์๋ค๊ณ ๋์์ ์ ๋ฅผ ๋จน์๊ณ , 2๋ฒ์งธ ๋ฌธ์ ๋ ์ฃผ๋ฌธํ์ธ!
์ด ๋ฒํผ์ด ๋์ค์ง ์์ ์ ๋ฅผ ๋จน์๋ค. ์ ์๋์ด ์๋ ค์ฃผ์ ๋๋ก ๊ทธ๋๋ก ํ๊ณ ๋ค๋ฅธ ๊ฒ ํ๋ ์์๋๋ฐ ๋ญ๊ฐ ๋ฌธ์ ์์๊น...
์ง์ ์์ ๋ค์ ํ์ธํด๋ณด๋๊น LAN ์ฐ๊ฒฐ์ด ์ฌ๋ฐ๋ฅด๊ฒ ๋๋์ง ์ ์์๋ํ์๋ค. ์ด์ ์ฃผ์ฐจ์์๋ ๊ฐ์ ๋๋ด๋๊ฒ ์์ํ๋๋ฐ 4์ฃผ์ฐจ๋ ์ฝ์ง๊ฐ ์๋ค.
์ ์ ๋ฐฐ์ ๋ ๊ฒ๋ค์ด ์ ์ ํ ๋์๋๋ฐ ๋ด๊ฐ ์ ๋๋ก ๋ณต์ต์ ์ํด์์ธ์ง ๋ฏ์ ์ต์ด๋ ์ด๋ป๊ฒ ํ์ฉํ๋ฉด ์ข์์ง๊ฐ ์ ์๊ฐ์ด ์ ๋ ์ฌ๋๋ค. ๊ทธ๋ฅ ์ ์๋ ์ฝ๋๋ฅผ ๊ทธ๋๋ก ๋ฐ๋ผ ํ๊ณ ์๋ ๋๋์ด๋ค. ์๊ฐ๊ธฐ๊ฐ์ด ์ผ์ฃผ์ผ๋ ์ ๋จ์๋๋ฐ ๋ฏธ์น๋ฏ์ด ๋ณต์ตํด์ผ ํ ๊ฒ ๊ฐ์ ๋๋์ด ๋ ๋ค.
์๋ฒ๊ฐ ์ python์ธ์ง ํด๋ผ์ด์ธํธ๊ฐ ์ html์ชฝ์ธ์ง๋ ์ ๋ชจ๋ฅด๊ฒ ๋ค. ๊ทธ๋๋ง ๋คํ์ธ๊ฑด ๋๋ต์ ์ธ ๋ผ๋๋ ์กฐ๊ธ ์ดํด๊ฐ ๋๋ค๋ ๊ฒ์ด๋ค. ๋๋จธ์ง ์ธ๋ถ์ ์ธ ๊ฑฐ๋ ๋ ํด๋ด์ผ ํ ๊ฒ ๊ฐ๋ค...
์์ : ํ๋ก์ ํธ ์ธํ -> ์กฐ๊ฐ ๊ธฐ์ฆ ๊ตฌํ -> ๋ผ๋ ์ค๋น -> POST ์ฐ์ต (ํฌ์คํ ํ๊ธฐ) -> GET ์ฐ์ต(๋ณด์ฌ์ฃผ๊ธฐ)
1. ๊ธฐ๋กํ๊ธฐ : url์์ ์ด๋ฏธ์ง, ์ ๋ชฉ, ๋์คํฌ๋ฆฝ์ ์ ํฌ๋กค๋งํ์ฌ DB์ ์ ์ฅํ๋ ๊ฒ์ด ํต์ฌ!)
2. ๋ณด์ฌ์ฃผ๊ธฐ : ์นด๋๋ฅผ ๋ง๋ค์ด์ ๋ถ์ฌ์ฃผ๊ธฐ์ธํ
flask์ DB์ฐ๊ฒฐ ํจํค์ง : flask, pymongo, dnspython
ํฌ๋กค๋ง ํจํค์ง : requests, bs4
์กฐ๊ฐ๊ธฐ๋ฅ : ํ๋ก์ ํธ๋ฅผ ๋ณธ๊ฒฉ์ ์ผ๋ก ๊ตฌํํ๊ธฐ ์ ์ ํ๋ก์ ํธ์ ํ์ํ ๊ธฐ์ ๋ค์ ๋จผ์ ๊ตฌํํด๋ณด๋๊ฒ ์ข์
meta ํ๊ทธ (๋ณด์ถฉ)
ํฌ๋กค๋ง ๊ธฐ๋ณธ์ฝ๋
import requests
from bs4 import BeautifulSoup
url = 'https://movie.naver.com/movie/bi/mi/basic.naver?code=191597'
headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}
data = requests.get(url,headers=headers)
soup = BeautifulSoup(data.text, 'html.parser')
# ์ฌ๊ธฐ์ ์ฝ๋ฉ์ ํด์ meta tag๋ฅผ ๋จผ์ ๊ฐ์ ธ์๋ณด๊ฒ ์ต๋๋ค.
์กฐ๊ฐ๊ธฐ๋ฅ ์์ฑ
import requests
from bs4 import BeautifulSoup
url = 'https://movie.naver.com/movie/bi/mi/basic.naver?code=191597'
headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}
data = requests.get(url,headers=headers)
soup = BeautifulSoup(data.text, 'html.parser')
title = soup.select_one('meta[property="og:title"]')['content']
image = soup.select_one('meta[property="og:image"]')['content']
desc = soup.select_one('meta[property="og:description"]')['content']
print(title, image, desc)
API ๋ง๋ค๊ณ ์ฌ์ฉํ๊ธฐ! Create -> POST
- ์์ฒญ ์ ๋ณด : URL= /movie, ์์ฒญ ๋ฐฉ์ = POST
- ํด๋ผ(ajax) โ ์๋ฒ(flask) :
url
,star
,comment
- ์๋ฒ(flask) โ ํด๋ผ(ajax) : ๋ฉ์์ง๋ฅผ ๋ณด๋ (ํฌ์คํ ์๋ฃ!)
(url๋ก ํฌ๋กค๋งํด์ image, title, description, star, comment๋ฅผ DB์ ์ ์ฅํ๊ณ ์๋ฃ!)
(1) ์ฐ๊ฒฐํ์ธ
(2) ์๋ฒ๋ถํฐ ๋ง๋ค๊ธฐ
app.py
@app.route("/movie", methods=["POST"])
def movie_post():
url_receive = request.form['url_give']
star_receive = request.form['star_give']
comment_receive = request.form['comment_give']
headers = {
'User-Ag ent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}
data = requests.get(url_receive, headers=headers)
soup = BeautifulSoup(data.text, 'html.parser')
title = soup.select_one('meta[property="og:title"]')['content']
image = soup.select_one('meta[property="og:image"]')['content']
desc = soup.select_one('meta[property="og:description"]')['content']
doc = {
'title': title,
'image': image,
'desc': desc,
'star': star_receive,
'comment': comment_receive
}
db.movies.insert_one(doc)
return jsonify({'msg':'์ ์ฅ ์๋ฃ!'})
(3) ํด๋ผ์ด์ธํธ ๋ง๋ค๊ธฐ
index.html
function posting() {
let url = $('#url').val()
let star = $('#star').val()
let comment = $('#comment').val()
$.ajax({
type: 'POST',
url: '/movie',
data: {url_give:url, star_give:star, comment_give:comment},
success: function (response) {
alert(response['msg'])
window.location.reload()
}
});
}
(4) ์์ฑํ์ธํ๊ธฐ
๐ API ๋ง๋ค๊ณ ์ฌ์ฉํ๊ธฐ - ๋ณด์ฌ์ฃผ๊ธฐAPI (Read โ GET)
1. ์์ฒญ ์ ๋ณด : URL=/movie
, ์์ฒญ ๋ฐฉ์ =GET
2. ํด๋ผ(ajax) โ ์๋ฒ(flask) : (์์)
3. ์๋ฒ(flask) โ ํด๋ผ(ajax) : ์ ์ฒด ์ํ๋ฅผ ๋ณด๋ด์ฃผ๊ธฐ
(1) ์ฐ๊ฒฐํ์ธ
(2) ์๋ฒ๋ถํฐ ๋ง๋ค๊ธฐ
orders
์ ์ฃผ๋ฌธ์ ๋ณด๋ฅผ ๋ด์์ ๋ด๋ ค์ฃผ๊ธฐapp.py
@app.route("/movie", methods=["GET"])
def movie_get():
movie_list = list(db.movies.find({},{'_id':False}))
return jsonify({'movies':movie_list})
(3) ํด๋ผ์ด์ธํธ ๋ง๋ค๊ธฐ
index.html
function listing() {
$('#cards-box').empty()
$.ajax({
type: 'GET',
url: '/movie',
data: {},
success: function (response) {
let rows = response['movies']
for(let i = 0; i < rows.length; i++) {
let image = rows[i]['image']
let title = rows[i]['title']
let desc = rows[i]['desc']
let star = rows[i]['star']
let comment = rows[i]['comment']
let star_image = 'โญ'.repeat(star)
let temp_html = `<div class="col">
<div class="card h-100">
<img src="${image}"
class="card-img-top">
<div class="card-body">
<h5 class="card-title">${title}</h5>
<p class="card-text">${desc}</p>
<p>${star_image}</p>
<p class="mycomment">${comment}</p>
</div>
</div>
</div>`
$('#cards-box').append(temp_html)
}
}
})
}
(4) ์์ฑํ์ธํ๊ธฐ
app.py
from flask import Flask, render_template, request, jsonify
app = Flask(__name__)
import requests
from bs4 import BeautifulSoup
from pymongo import MongoClient
client = MongoClient('mongodb+srv://minjeong:rPfksakfdl1!@cluster0.mtbw8.mongodb.net/myFirstDatabase?retryWrites=true&w=majority')
db = client.dbsparta
@app.route('/')
def home():
return render_template('index.html')
@app.route("/movie", methods=["POST"])
def movie_post():
url_receive = request.form['url_give']
star_receive = request.form['star_give']
comment_receive = request.form['comment_give']
headers = {
'User-Ag ent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}
data = requests.get(url_receive, headers=headers)
soup = BeautifulSoup(data.text, 'html.parser')
title = soup.select_one('meta[property="og:title"]')['content']
image = soup.select_one('meta[property="og:image"]')['content']
desc = soup.select_one('meta[property="og:description"]')['content']
doc = {
'title': title,
'image': image,
'desc': desc,
'star': star_receive,
'comment': comment_receive
}
db.movies.insert_one(doc)
return jsonify({'msg':'์ ์ฅ ์๋ฃ!'})
@app.route("/movie", methods=["GET"])
def movie_get():
movie_list = list(db.movies.find({}, {'_id': False}))
return jsonify({'movies':movie_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 name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<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>
<title>์คํ๋ฅดํ ํผ๋์</title>
<link href="https://fonts.googleapis.com/css2?family=Gowun+Dodum&display=swap" rel="stylesheet">
<style>
* {
font-family: 'Gowun Dodum', sans-serif;
}
.mytitle {
width: 100%;
height: 250px;
background-image: linear-gradient(0deg, rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url('https://movie-phinf.pstatic.net/20210715_95/1626338192428gTnJl_JPEG/movie_image.jpg');
background-position: center;
background-size: cover;
color: white;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.mytitle > button {
width: 200px;
height: 50px;
background-color: transparent;
color: white;
border-radius: 50px;
border: 1px solid white;
margin-top: 10px;
}
.mytitle > button:hover {
border: 2px solid white;
}
.mycomment {
color: gray;
}
.mycards {
margin: 20px auto 0px auto;
width: 95%;
max-width: 1200px;
}
.mypost {
width: 95%;
max-width: 500px;
margin: 20px auto 0px auto;
padding: 20px;
box-shadow: 0px 0px 3px 0px gray;
display: none;
}
.mybtns {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
margin-top: 20px;
}
.mybtns > button {
margin-right: 10px;
}
</style>
<script>
$(document).ready(function () {
listing();
});
function listing() {
$.ajax({
type: 'GET',
url: '/movie',
data: {},
success: function (response) {
let rows = response['movies']
for (let i = 0; i < rows.length; i++){
let comment = rows[i]['comment']
let title = rows[i]['title']
let desc = rows[i]['desc']
let image = rows[i]['image']
let star = rows[i]['star']
let star_image = 'โญ'.repeat(star)
let temp_html = `<div class="col">
<div class="card h-100">
<img src="${image}"
class="card-img-top">
<div class="card-body">
<h5 class="card-title">${title}</h5>
<p class="card-text">${desc}</p>
<p>${star_image}</p>
<p class="mycomment">${comment}</p>
</div>
</div>
</div>`
$('#cards-box').append(temp_html)
}
}
})
}
function posting() {
let url = $('#url').val()
let star = $('#star').val()
let comment = $('#comment').val()
$.ajax({
type: 'POST',
url: '/movie',
data: {url_give:url, star_give:star, comment_give:comment},
success: function (response) {
alert(response['msg'])
window.location.reload()
}
});
}
function open_box() {
$('#post-box').show()
}
function close_box() {
$('#post-box').hide()
}
</script>
</head>
<body>
<div class="mytitle">
<h1>๋ด ์์ ์ต๊ณ ์ ์ํ๋ค</h1>
<button onclick="open_box()">์ํ ๊ธฐ๋กํ๊ธฐ</button>
</div>
<div class="mypost" id="post-box">
<div class="form-floating mb-3">
<input id="url" type="email" class="form-control" placeholder="name@example.com">
<label>์ํURL</label>
</div>
<div class="input-group mb-3">
<label class="input-group-text" for="inputGroupSelect01">๋ณ์ </label>
<select class="form-select" id="star">
<option selected>-- ์ ํํ๊ธฐ --</option>
<option value="1">โญ</option>
<option value="2">โญโญ</option>
<option value="3">โญโญโญ</option>
<option value="4">โญโญโญโญ</option>
<option value="5">โญโญโญโญโญ</option>
</select>
</div>
<div class="form-floating">
<textarea id="comment" class="form-control" placeholder="Leave a comment here"></textarea>
<label for="floatingTextarea2">์ฝ๋ฉํธ</label>
</div>
<div class="mybtns">
<button onclick="posting()" type="button" class="btn btn-dark">๊ธฐ๋กํ๊ธฐ</button>
<button onclick="close_box()" type="button" class="btn btn-outline-dark">๋ซ๊ธฐ</button>
</div>
</div>
<div class="mycards">
<div class="row row-cols-1 row-cols-md-4 g-4" id="cards-box">
</div>
</div>
</body>
</html>
์ ๋๊ฐ์ ์ค์ ๋ ๋ฐ๋ณตํ๋ค.
๋ ์คํ๋ ์ ๋ด๊ณ ์ ๋๋ก ํ๋๋ฐ ์ด๊ฒ ๋ญ์ง!? ํ๋๋ฐ,, templatesํด๋ ์์ index.htmlํ์ผ์ ์ ๋ฃ์๋ค. ๋ฐ์ฑ..
์ฐ๊ฒฐํ๋๊น ๋ฐ๋ก ๋๋ค.
app.py
from flask import Flask, render_template, request, jsonify
app = Flask(__name__)
from pymongo import MongoClient
client = MongoClient('mongodb+srv://minjeong:rPfksakfdl1!@cluster0.mtbw8.mongodb.net/myFirstDatabase?retryWrites=true&w=majority')
db = client.dbsparta
@app.route('/')
def home():
return render_template('index.html')
@app.route("/homework", methods=["POST"])
def homework_post():
name_receive = request.form['name_give']
comment_receive = request.form['comment_give']
doc = {
'name': name_receive,
'comment': comment_receive
}
db.homework.insert_one(doc)
return jsonify({'msg':'์
๋ ฅ ์๋ฃ!'})
@app.route("/homework", methods=["GET"])
def homework_get():
all_comments = list(db.homework.find({}, {'_id': False}))
return jsonify({'comments': all_comments})
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>
<title>์์ด์ ํฌ๋ช
๋ก</title>
<link href="https://fonts.googleapis.com/css2?family=Noto+Serif+KR:wght@200;300;400;500;600;700;900&display=swap"
rel="stylesheet">
<style>
* {
font-family: 'Noto Serif KR', serif;
}
.mypic {
width: 100%;
height: 300px;
background-image: linear-gradient(0deg, rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url('https://t1.daumcdn.net/cfile/tistory/9960CC425BFA32EF19');
background-position: center 10%;
background-size: cover;
color: white;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.mypost {
width: 95%;
max-width: 500px;
margin: 20px auto 20px auto;
box-shadow: 0px 0px 3px 0px black;
padding: 20px;
}
.mypost > button {
margin-top: 15px;
}
.mycards {
width: 95%;
max-width: 500px;
margin: auto;
}
.mycards > .card {
margin-top: 10px;
margin-bottom: 10px;
}
</style>
<script>
$(document).ready(function () {
set_temp()
show_comment()
});
function set_temp() {
$.ajax({
type: "GET",
url: "http://spartacodingclub.shop/sparta_api/weather/seoul",
data: {},
success: function (response) {
$('#temp').text(response['temp'])
}
})
}
function save_comment() {
let name = $('#name').val()
let comment = $('#comment').val()
$.ajax({
type: 'POST',
url: '/homework',
data: {name_give: name, comment_give: comment},
success: function (response) {
alert(response['msg'])
window.location.reload()
}
});
}
function show_comment() {
$.ajax({
type: "GET",
url: "/homework",
data: {},
success: function (response) {
let rows = response['comments']
for (let i = 0; i < rows.length; i++){
let name = rows[i]['name']
let comment = rows[i]['comment']
let temp_html = `<div class="card">
<div class="card-body">
<blockquote class="blockquote mb-0">
<p>${comment}</p>
<footer class="blockquote-footer">${name}</footer>
</blockquote>
</div>
</div>`
$(`#comment-list`).append(temp_html)
}
}
});
}
</script>
</head>
<body>
<div class="mypic">
<h1>์์ด์ ํฌ๋ช
๋ก</h1>
<p>ํ์ฌ๊ธฐ์จ: <span id="temp">36</span>๋</p>
</div>
<div class="mypost">
<div class="form-floating mb-3">
<input type="text" class="form-control" id="name" placeholder="url">
<label for="floatingInput">๋๋ค์</label>
</div>
<div class="form-floating">
<textarea class="form-control" placeholder="Leave a comment here" id="comment"
style="height: 100px"></textarea>
<label for="floatingTextarea2">์์๋๊ธ</label>
</div>
<button onclick="save_comment()" type="button" class="btn btn-dark">์์ ๋จ๊ธฐ๊ธฐ</button>
</div>
<div class="mycards" id="comment-list">
</div>
</body>
</html>
์ข์๋ ์ : ์ด๋ ค์ด ํํธ์ฒ๋ผ ๋๊ปด์ก๋๋ฐ, ๊ทธ๋งํผ ์ฌ๋ฌ๋ฒ ์์ ๋ฅผ ํ์ด๋ด์ ์ข์๋ค!
์์ฌ์ ๋ ์ : ์์ฌ์ด ์ ์ ๋ฑํ ์๋ค. ๊ตณ์ด ๊ผฝ์๋ฉด ์จ๋ผ์ธ์ด๋ผ๋ ์ ..?!
์ ๋
1๋ฒ ์์ , 2๋ฒ ์์ , ์์ ๋ชจ๋ ๊ฐ์ ๋ถ๋ถ์์ ํค๋งธ๋๋ฐ ์ด๋ค ๊ฑด์ง ์ ํํ ์์ธ์ ๋ชจ๋ฅด๊ณ ์ผ๋ ๋ฑ๋
ํด๊ฒฐ๋ ๊ฒฝ์ฐ๊ฐ ๋ง์๋ค. ๊ทธ๋์ ์ง๋ฌธํ๊ธฐ๋ ์ด๋ ต๊ณ ๋ญ๊ฐ ๋ฌธ์ ์๋์ง ์๊ธฐ๋ ์ด๋ ต๊ณ ,, ๊ทธ๋ฐ ๊ฒ ๊ฐ๋ค! ๋์ฒด์ ์ผ๋ก๋ ๋ค ๋ง์กฑํ๋ค!
์ถ์ฒ : ๐๐๐๐๐