(4์ฃผ์ฐจ๋Š” ์ด‰๋ฐ•ํ•˜๊ฒŒ ๋“ค์–ด์„œ ,, ์ •๋ฆฌ๊ฐ€ ์ž˜ ์•ˆ ๋˜์–ด ์žˆ์Œ ์ฃผ์˜!)
4์ฃผ์ฐจ ์ˆ˜์—… : ์„œ๋ฒ„ ๋งŒ๋“ค๊ธฐ
์ฝ”๋“œ ๊ด€๋ฆฌ ์œ„ํ•ด ํด๋”๊ตฌ์กฐ ๋งŒ๋“ค์—ˆ์Œ

1. flask ํ”„๋ ˆ์ž„์›Œํฌ

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 ํ”„๋ ˆ์ž„์›Œํฌ ์‚ฌ์šฉ
๊ธฐ๋ณธ ํด๋” ๊ตฌ์กฐ

  • tempelates : htmlํŒŒ์ผ ๋‹ด๋Š” ๊ณณ
  • static : ์ด๋ฏธ์ง€, cssํŒŒ์ผ ๋“ฑ ๋‹ด๋Š” ๊ณณ

2. API ๋งŒ๋“ค๊ธฐ

  • GET ๋ฐฉ์‹ -> ๋ฐ์ดํ„ฐ ์กฐํšŒํ•  ๋•Œ
  • POST ๋ฐฉ์‹ -> ๋ฐ์ดํ„ฐ ์ƒ์„ฑ, ๋ณ€๊ฒฝ, ์‚ญ์ œํ•  ๋•Œ
  1. jquery ์ž„ํฌํŠธ
<head>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>

4-4 ๊ฐ•์˜๋ถ€๋ถ„ ์ด๋”ฐ ๋‹ค์‹œ ๋ณด์ถฉํ•˜๊ธฐ
์–ด๋ ค์›Œ์„œ ๋’ค์— ์˜ˆ์ œ 4๊ฐœ๊ฐ€ ์ค€๋น„ ๋˜์–ด ์žˆ์Œ!!!


3. ์˜ˆ์ œ(1) - ํ™”์„ฑ๋•… ๊ณต๋™๊ตฌ๋งค

๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ 1. ์ฃผ๋ฌธํ•˜๊ธฐ (POST) 2. ์ฃผ๋ฌธ ๋ณด์—ฌ์ฃผ๊ธฐ(GET)

POST :: ์ฃผ๋ฌธ์ €์žฅ

๐Ÿ‘‰ API ๋งŒ๋“ค๊ณ  ์‚ฌ์šฉํ•˜๊ธฐ! - ์ด๋ฆ„, ์ฃผ์†Œ, ํ‰์ˆ˜ ์ €์žฅ (Creat -> POST)
1. ์š”์ฒญ ์ •๋ณด : URL= /mars, ์š”์ฒญ ๋ฐฉ์‹ = POST
2. ํด๋ผ(ajax) โ†’ ์„œ๋ฒ„(flask) : name, address, size
3. ์„œ๋ฒ„(flask) โ†’ ํด๋ผ(ajax) : ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋ƒ„ (์ฃผ๋ฌธ ์™„๋ฃŒ!)

(1) ์—ฐ๊ฒฐํ™•์ธํ•˜๊ธฐ

(2) ์„œ๋ฒ„ ๋งŒ๋“ค๊ธฐ (app.py)

  • name, address, size์˜ ์ •๋ณด๋ฅผ ๋ฐ›์•„์„œ ์ €์žฅ!
  • db์—ฐ๊ฒฐํ•˜๊ธฐ
@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)

์˜ˆ์ œ1ํ•˜๊ณ  ๋Š๋‚€ ์ 

์—ฌ๊ธฐ๊นŒ์ง€๊ฐ€ 7๊ฐ•์ด์—ˆ๋Š”๋ฐ ๊ณ„์† ๋ง‰ํ˜”๋‹ค. 1๋ฒˆ์งธ ๋ฌธ์ œ๋Š” ์ง‘์—์„œ ๋“ฃ๋‹ค๊ฐ€ ์นดํŽ˜์—์„œ ๋“ฃ๋Š”๋ฐ LAN ์—ฐ๊ฒฐ์ด ์˜ณ์ง€ ์•Š๋‹ค๊ณ  ๋‚˜์™€์„œ ์• ๋ฅผ ๋จน์—ˆ๊ณ , 2๋ฒˆ์งธ ๋ฌธ์ œ๋Š” ์ฃผ๋ฌธํ™•์ธ! ์ด ๋ฒ„ํŠผ์ด ๋‚˜์˜ค์ง€ ์•Š์•„ ์• ๋ฅผ ๋จน์—ˆ๋‹ค. ์„ ์ƒ๋‹˜์ด ์•Œ๋ ค์ฃผ์‹ ๋Œ€๋กœ ๊ทธ๋Œ€๋กœ ํ•˜๊ณ  ๋‹ค๋ฅธ ๊ฒƒ ํ•˜๋‚˜ ์—†์—ˆ๋Š”๋ฐ ๋ญ๊ฐ€ ๋ฌธ์ œ์˜€์„๊นŒ...
์ง‘์— ์™€์„œ ๋‹ค์‹œ ํ™•์ธํ•ด๋ณด๋‹ˆ๊นŒ LAN ์—ฐ๊ฒฐ์ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋๋Š”์ง€ ์ •์ƒ์ž‘๋™ํ•˜์˜€๋‹ค. ์ด์ „ ์ฃผ์ฐจ์—์„œ๋Š” ๊ฐ•์˜ ๋๋‚ด๋Š”๊ฒŒ ์ˆ˜์›”ํ–ˆ๋Š”๋ฐ 4์ฃผ์ฐจ๋Š” ์‰ฝ์ง€๊ฐ€ ์•Š๋‹ค.

์ „์— ๋ฐฐ์› ๋˜ ๊ฒƒ๋“ค์ด ์ ์ ํžˆ ๋‚˜์™”๋Š”๋ฐ ๋‚ด๊ฐ€ ์ œ๋Œ€๋กœ ๋ณต์Šต์„ ์•ˆํ•ด์„œ์ธ์ง€ ๋‚ฏ์€ ์ต์–ด๋„ ์–ด๋–ป๊ฒŒ ํ™œ์šฉํ•˜๋ฉด ์ข‹์„์ง€๊ฐ€ ์ž˜ ์ƒ๊ฐ์ด ์•ˆ ๋– ์˜ฌ๋ž๋‹ค. ๊ทธ๋ƒฅ ์„ ์ƒ๋‹˜ ์ฝ”๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ๋”ฐ๋ผ ํ•˜๊ณ  ์žˆ๋Š” ๋Š๋‚Œ์ด๋‹ค. ์ˆ˜๊ฐ•๊ธฐ๊ฐ„์ด ์ผ์ฃผ์ผ๋„ ์•ˆ ๋‚จ์•˜๋Š”๋ฐ ๋ฏธ์นœ๋“ฏ์ด ๋ณต์Šตํ•ด์•ผ ํ•  ๊ฒƒ ๊ฐ™์€ ๋Š๋‚Œ์ด ๋“ ๋‹ค.
์„œ๋ฒ„๊ฐ€ ์™œ python์ธ์ง€ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์™œ html์ชฝ์ธ์ง€๋„ ์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค. ๊ทธ๋‚˜๋งˆ ๋‹คํ–‰์ธ๊ฑด ๋Œ€๋žต์ ์ธ ๋ผˆ๋Œ€๋Š” ์กฐ๊ธˆ ์ดํ•ด๊ฐ€ ๋œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋‚˜๋จธ์ง€ ์„ธ๋ถ€์ ์ธ ๊ฑฐ๋Š” ๋” ํ•ด๋ด์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค...


์˜ˆ์ œ 2. [์ŠคํŒŒ๋ฅดํƒ€ํ”ผ๋””์•„]

์ˆœ์„œ : ํ”„๋กœ์ ํŠธ ์„ธํŒ… -> ์กฐ๊ฐ ๊ธฐ์ฆ ๊ตฌํ˜„ -> ๋ผˆ๋Œ€ ์ค€๋น„ -> 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 ๋งŒ๋“ค๊ธฐ ์‹œ์ž‘!

  1. ๋ผˆ๋Œ€ ๋งŒ๋“ค๊ธฐ
  2. POST ๊ตฌํ˜„
  3. GET ๊ตฌํ˜„

1. POST :: ํฌ์ŠคํŒ…ํ•˜๊ธฐ

API ๋งŒ๋“ค๊ณ  ์‚ฌ์šฉํ•˜๊ธฐ! Create -> POST

  1. ์š”์ฒญ ์ •๋ณด : URL= /movie, ์š”์ฒญ ๋ฐฉ์‹ = POST
  2. ํด๋ผ(ajax) โ†’ ์„œ๋ฒ„(flask) : url, star, comment
  3. ์„œ๋ฒ„(flask) โ†’ ํด๋ผ(ajax) : ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋ƒ„ (ํฌ์ŠคํŒ… ์™„๋ฃŒ!)
    (url๋กœ ํฌ๋กค๋งํ•ด์„œ image, title, description, star, comment๋ฅผ DB์— ์ €์žฅํ•˜๊ณ  ์™„๋ฃŒ!)

(1) ์—ฐ๊ฒฐํ™•์ธ

(2) ์„œ๋ฒ„๋ถ€ํ„ฐ ๋งŒ๋“ค๊ธฐ

  • url, star, comment ์ •๋ณด๋ฅผ ๋ฐ›์•„์„œ(receive) ์ €์žฅ!
  • ๋ฏธ๋ฆฌ ๋งŒ๋“  meta_prac.py๋„ ๋ถ™์—ฌ๋„ฃ๊ธฐ!
  • dbtest.py๋ฅผ ์ด์šฉํ•˜์—ฌ pymongo ์—ฐ๊ฒฐ

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) ํด๋ผ์ด์–ธํŠธ ๋งŒ๋“ค๊ธฐ

  • url, star, comment ์ •๋ณด ๋ณด๋‚ด์ฃผ๊ธฐ (give)

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) ์™„์„ฑํ™•์ธํ•˜๊ธฐ

2. GET :: ๋ณด์—ฌ์ฃผ๊ธฐ

๐Ÿ‘‰ 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) ํด๋ผ์ด์–ธํŠธ ๋งŒ๋“ค๊ธฐ

  • ์‘๋‹ต ๋ฐ›์•„์„œ for๋ฌธ์œผ๋กœ ๋Œ๋ ค์ฃผ๊ธฐ

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>

์˜ˆ์ฉจ 2 - ๋Š๋‚€ ์ 

์•„ ๋˜‘๊ฐ™์€ ์‹ค์ˆ˜ ๋˜ ๋ฐ˜๋ณตํ–ˆ๋‹ค.

๋‚œ ์˜คํƒ€๋„ ์•ˆ ๋‚ด๊ณ  ์ œ๋Œ€๋กœ ํ–ˆ๋Š”๋ฐ ์ด๊ฒŒ ๋ญ์ง€!? ํ–ˆ๋Š”๋ฐ,, 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๋ฒˆ ์˜ˆ์ œ, ์ˆ™์ œ ๋ชจ๋‘ ๊ฐ™์€ ๋ถ€๋ถ„์—์„œ ํ—ค๋งธ๋Š”๋ฐ ์–ด๋–ค ๊ฑด์ง€ ์ •ํ™•ํ•œ ์›์ธ์„ ๋ชจ๋ฅด๊ณ  ์–ผ๋ ๋šฑ๋•… ํ•ด๊ฒฐ๋œ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์•˜๋‹ค. ๊ทธ๋ž˜์„œ ์งˆ๋ฌธํ•˜๊ธฐ๋„ ์–ด๋ ต๊ณ  ๋ญ๊ฐ€ ๋ฌธ์ œ์˜€๋Š”์ง€ ์•Œ๊ธฐ๋„ ์–ด๋ ต๊ณ ,, ๊ทธ๋Ÿฐ ๊ฒƒ ๊ฐ™๋‹ค! ๋Œ€์ฒด์ ์œผ๋กœ๋Š” ๋‹ค ๋งŒ์กฑํ•œ๋‹ค!

์ถ”์ฒœ : ๐ŸŒ•๐ŸŒ•๐ŸŒ•๐ŸŒ•๐ŸŒ‘

profile
To be "irreplaceable"

0๊ฐœ์˜ ๋Œ“๊ธ€

๊ด€๋ จ ์ฑ„์šฉ ์ •๋ณด