๐Ÿ‘ถ ๋‚ด์ผ๋ฐฐ์›€๋‹จ/์›น๊ฐœ๋ฐœ ์ข…ํ•ฉ ๊ฐœ๋ฐœ์ผ์ง€

[์ŠคํŒŒ๋ฅดํƒ€์ฝ”๋”ฉํด๋Ÿฝ] ์›น๊ฐœ๋ฐœ ์ข…ํ•ฉ๋ฐ˜ - 4์ฃผ์ฐจ ๊ฐœ๋ฐœ์ผ์ง€ / ์ˆ™์ œ

  • -

๋“œ๋””์–ด 1์ฃผ์ฐจ ๊ณต๋“ค์ธ ์ˆ™์ œ๋ฅผ ์™„์„ฑํ•  ๋•Œ๊ฐ€ ์™”๋‹ค.

 

1์ฃผ์ฐจ์— ์™„์„ฑํ•œ ์‡ผํ•‘๋ชฐ์„ ์™„์„ฑํ•ด์ฃผ์„ธ์š”!

 

์‡ผํ•‘๋ชฐ์€ ๋‘ ๊ฐ€์ง€ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

  1) ์ฃผ๋ฌธํ•˜๊ธฐ(POST): ์ •๋ณด ์ž…๋ ฅ ํ›„ '์ฃผ๋ฌธํ•˜๊ธฐ' ๋ฒ„ํŠผํด๋ฆญ ์‹œ ์ฃผ๋ฌธ๋ชฉ๋ก์— ์ถ”๊ฐ€

  2) ์ฃผ๋ฌธ๋‚ด์—ญ๋ณด๊ธฐ(GET): ํŽ˜์ด์ง€ ๋กœ๋”ฉ ํ›„ ํ•˜๋‹จ ์ฃผ๋ฌธ ๋ชฉ๋ก์ด ์ž๋™์œผ๋กœ ๋ณด์ด๊ธฐ

 

์‚ฌ์‹ค 1์ฃผ์ฐจ ๊ธ€์„ ๋ณด๋ฉด ์•Œ ์ˆ˜ ์žˆ์ง€๋งŒ

 

๋ฉ˜ํƒˆ ํ„ฐ์ง„ ๊ธ€ ๋‚ด์šฉ๊ณผ ๋ญ˜ ๋งŒ๋“ค์—ˆ๋Š”์ง€ ๋ชจ๋ฅผ ๊ณต๋ฐฑ์ด ๊ฐ€๋“ํ•˜๋‹ค.

 

์ด์œ ๋Š” ์•Œ๋‹ค์‹œํ”ผ 1์ฃผ์— ๋ญ˜ ๋‹ด์„ ์ˆ˜ ์žˆ์—ˆ์„๊นŒ..

 

๊ฐˆ ๊ณณ์„ ์žƒ์€ ๋งˆ์šฐ์Šค์™€ ์„ธ์ƒ ๋Š๋ฆฐ ๊ฑฐ๋ถ์ด ํƒ€์ž๋กœ ๊ฒจ์šฐ ๋”ฐ๋ผ๊ฐ„๊ฒŒ ์ „๋ถ€์˜€๋‹ค.

 

ํ•˜์ง€๋งŒ ๋งŽ์€ ์‹œ๋„์™€ ์‹ค์‹œ๊ฐ„ ๋ณต์Šต์„ ํ†ตํ•ด ์–ด๋Š์ •๋„ ๋”ฐ๋ผ์™”์Œ์„ ์Šค์Šค๋กœ ๋Š๋ผ๊ฒŒ ๋˜์—ˆ๋‹ค.

 

์ „๋ณด๋‹ค ์„ฑ์žฅํ•œ ์ž์‹ ์„ ๋ณด๋ฉฐ ๊ณ„์† ๋‚˜์•„๊ฐ€์ž.

 

 


 

app

from flask import Flask, render_template, jsonify, request

app = Flask(__name__)

from pymongo import MongoClient

client = MongoClient('localhost', 27017)
db = client.dbhomework


## HTML ํ™”๋ฉด ๋ณด์—ฌ์ฃผ๊ธฐ
@app.route('/')
def homework():
    return render_template('index.html')


# ์ฃผ๋ฌธํ•˜๊ธฐ(POST) API
@app.route('/order', methods=['POST'])
def save_order():
    name_receive = request.form['name_give']
    count_receive = request.form['count_give']
    add_receive = request.form['add_give']
    number_receive = request.form['number_give']

    doc = {
        'name': name_receive,
        'count': count_receive,
        'add': add_receive,
        'number': number_receive
    }

    db.myshop.insert_one(doc)

    return jsonify({'msg': '์ฃผ๋ฌธ์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!'})


# ์ฃผ๋ฌธ ๋ชฉ๋ก๋ณด๊ธฐ(Read) API
@app.route('/order', methods=['GET'])
def view_orders():
    customers = list(db.myshop.find({}, {'_id': False}))
    return jsonify({'all_customers':customers})


if __name__ == '__main__':
    app.run('0.0.0.0', port=5000, debug=True)

 

 

html

<!doctype html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
          integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
            integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
            crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
            integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
            crossorigin="anonymous"></script>

    <title>์–‘์ดˆ์บ”๋“ค | ๊ตฌ๋งค ํŽ˜์ด์ง€</title>
    <link href="https://fonts.googleapis.com/css2?family=Do+Hyeon&family=Nanum+Pen+Script&display=swap"
          rel="stylesheet">
    <style>
        * {
            font-family: 'Nanum Pen Script', cursive;
        }
        .tablestyle {
            width: 480px;
            margin: 20px auto 30px auto;
        }
        .img_box {
            width: 480px;
            margin: 20px auto 30px auto;
        }
        .mybtn {
            width: 100px;
            margin: auto;
            display: block;
        }
        .dollar {
            color: blue;
        }
    </style>
    <script>
        $(document).ready(function () {
            q1(),
            list()
        });

        function q1() {
            $.ajax({
                type: "GET",
                url: "http://spartacodingclub.shop/sparta_api/rate",
                data: {},
                success: function (response) {
                    let doller = response[`rate`]

                    let temp_html = `<h3 class="dollar">๋‹ฌ๋Ÿฌ : ์› / ํ™˜์œจ : ${doller}</h3>`
                    $('#now_doller').append(temp_html)
                }
            })
        }

        function order() {
            let name = $('#post-name').val()
            let count = $('#inputGroupSelect01').val()
            let add = $('#post-add').val()
            let number = $('#post-number').val()

            $.ajax({
                type: "POST",
                url: "/order",
                data: {name_give:name, count_give:count, add_give:add, number_give:number},
                success: function (response) { // ์„ฑ๊ณตํ•˜๋ฉด
                    alert(response["msg"]);
                    window.location.reload()
                }
            })
        }

        function list() {
                $.ajax({
                    type: "GET",
                    url: "/order",
                    data: {},
                    success: function (response) {
                        let customers = response['all_customers']
                        for (let i = 0; i < customers.length; i++) {
                            let name = customers[i]['name']
                            let count = customers[i]['count']
                            let add = customers[i]['add']
                            let number = customers[i]['number']

                            let temp_html = `<tr>
                                                <th scope="row">${name}</th>
                                                <td>${count}</td>
                                                <td>${add}</td>
                                                <td>${number}</td>
                                            </tr>`
                            $('#inputGroupSelect-list').append(temp_html)
                        }
                    }
                })
            }
    </script>
</head>

<body>
    <div class="img_box">
        <p>
            <img src="https://t1.daumcdn.net/cfile/tistory/2564EF4E55D681B320"/>
        </p>
        <h1 style="font-size:50px">์–‘์ดˆ ์บ”๋“ค ํŒ๋งค!! <span style="font-size: 30px">๊ฐ€๊ฒฉ:3,000์›/๊ฐœ</span></h1>
        <h2 style="font-size: 22px"> ์ด ์–‘์ดˆ๋Š” ์‚ฌ์‹ค ํŠน๋ณ„ํ•œ ํž˜์„ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์š”. ์–‘์ดˆ๋ฅผ ์ผœ๊ณ  ์†Œ์›์„ ๋นŒ๋ฉด ์งœ์ž๋ž€ ๋ญ๋“ ์ง€ ์ด๋ค„์ง€๊ฒŒ ๋œ๋‹ต๋‹ˆ๋‹ค. ํ•˜๋‚˜ ์‚ฌ๊ฐ€์„ธ์š”! ๋Œ€๋‚˜๋ฌด ํ–ฅ์ด ์•„์ฃผ
            ์ข‹์•„์š”!!
        </h2>

        <h3 id="now_doller"></h3>

        <div class="input-group input-group-sm mb-3">
            <div class="input-group-prepend">
                <span class="input-group-text" id="inputGroup-sizing-sm">์ฃผ๋ฌธ์ž ์ด๋ฆ„</span>
            </div>
            <input id="post-name" type="text" class="form-control" aria-label="Small" aria-describedby="inputGroup-sizing-sm">
        </div>
        <div class="input-group mb-3">
            <div class="input-group-prepend">
                <label class="input-group-text" for="inputGroupSelect01">์ˆ˜๋Ÿ‰</label>
            </div>
            <select class="custom-select" id="inputGroupSelect01">
                <option selected></option>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
            </select>
        </div>
        <div class="input-group input-group-sm mb-3">
            <div class="input-group-prepend">
                <span class="input-group-text" id="inputGroup-sizing-sm">์ฃผ์†Œ</span>
            </div>
            <input id="post-add" type="text" class="form-control" aria-label="Small" aria-describedby="inputGroup-sizing-sm">
        </div>
        <div class="input-group input-group-sm mb-3">
            <div class="input-group-prepend">
                <span class="input-group-text" id="inputGroup-sizing-sm">์ „ํ™”๋ฒˆํ˜ธ</span>
            </div>
            <input id="post-number" type="text" class="form-control" aria-label="Small" aria-describedby="inputGroup-sizing-sm">
        </div>
        <div class="mybtn">
            <button onclick="order()" type="button" class="btn btn-primary">์ฃผ๋ฌธํ•˜๊ธฐ</button>
        </div>
    </div>
    <table class="table tablestyle" id="inputGroupSelect-list">
        <thead>
        <tr>
            <th scope="col">์ฃผ๋ฌธ์ž๋ช…</th>
            <th scope="col">์ˆ˜๋Ÿ‰</th>
            <th scope="col">์ฃผ์†Œ</th>
            <th scope="col">์ „ํ™”๋ฒˆํ˜ธ</th>
        </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
</body>

</html>

 

 

 

์™„์„ฑ!!

1์ฃผ์ฐจ์— ๋งŒ๋“  ์›นํŽ˜์ด์ง€ > 4์ฃผ์ฐจ ๊ณผ์ •์„ ๋‹ด์€ ์›นํŽ˜์ด์ง€

 

์ˆ™์ œ๋ฅผ ํ•˜๋ฉด์„œ ๋Š๋‚€์ ๋“ค

 

1) ์ด๋ฆ„์„ ์ž˜ ์ง€์–ด์ฃผ์ž.

 ์ƒ๊ฐ๋ณด๋‹ค ์ด๋ฆ„์ด ๋“ค์–ด๊ฐ€๋Š” ์ž๋ฆฌ๊ฐ€ ๋งŽ๋‹ค. ๊ทผ๋ฐ ์ด๋ฆ„ ํ•˜๋‚˜ ์ œ๋Œ€๋กœ ์ž˜ ์•ˆ์ง€์–ด์ฃผ๊ฒŒ ๋œ๋‹ค๋ฉด, ๋‚ด๊ฐ€ ํ—ท๊ฐˆ๋ฆฌ๊ธฐ ์‹œ์ž‘ํ•œ๋‹ค. ์ฝ”๋”ฉ์„ ํ•˜๋ฉด์„œ ๋„ค์ด๋ฐ ์„ผ์Šค๊ฐ€ ํ•„์ˆ˜์‚ฌํ•ญ์ด๋ผ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

2) ๊ฒ‰ํ•ฅ๊ธฐ ์‹์ด์–ด๋„ ๊ดœ์ฐฎ์•„, ์ผ๋‹จ ๋”ฐ๋ผํ•ด๋ด.

 ํ€ด์ฆˆ๋‚˜ ์ˆ™์ œ๋ฅผ ํ•˜๋ฉด์„œ ๋Š๋‚€๊ฑฐ์ง€๋งŒ, ๋‚˜๋Š” ๊ณ„์† ๋นˆ ๊ณต๋ฐฑ์—์„œ ์‹œ์ž‘ํ•ด์•ผ ๋ฐฐ์› ๋˜๊ฑธ ์ž˜ ์จ๋จน์„ ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ ์ƒ๊ฐํ–ˆ๋‹ค. ํฐ ์˜ค์‚ฐ์ด๊ณ  ์ž๋งŒ์ด์˜€๋‹ค. ๋น„์ „๊ณต์ž ์™•์ดˆ๋ณด์—๊ฒ ๊ทธ๋Ÿฐ ๊ธฐ์ ๊ฐ™์€ ์ผ์€ ์ผ์–ด๋‚˜์ง€ ์•Š์•˜๊ณ , ์ด๊ธฐ์ ์ธ ์š•์‹ฌ์ด์˜€๋‹ค. ๊ทธ๋Ÿฐ ์ƒ๊ฐ์„ ์ ‘๊ณ  ์ผ๋‹จ ๋ฐฐ์› ๋˜๊ฑฐ ์œ„์ฃผ๋กœ ์ˆ˜์—…์œผ๋กœ ๊ฐ™์ด ์ฝ”๋”ฉ ํ–ˆ๋˜ ๊ฒƒ์„ ๋ณด๊ณ  ๋”ฐ๋ผ ์“ฐ๊ณ ๋ฅผ ๋ฐ˜๋ณตํ–ˆ๋‹ค. ๋‹น์—ฐํžˆ ๋ญ˜ ๋„ฃ์–ด์•ผํ•˜๊ณ  ๋“ค์–ด๊ฐ€์•ผํ•˜๊ณ  ์ง€์šฐ๊ณ  ๋ฐ”๊ฟ”์•ผํ•˜๋Š”์ง€ ๋ชจ๋ฅด๋Š”๊ฒŒ ํƒœ๋ฐ˜์ด์ง€๋งŒ, 4์ฃผ์ฐจ๋ฅผ ๋งˆ๋ฌด๋ฆฌํ•˜๊ณ  ์žˆ๋˜ ๋‚˜๋Š” ์ƒ๊ฐ๋ณด๋‹ค ๊ทธ ๊ณผ์ •๋“ค์„ ์ž˜ ์†Œํ™”ํ•˜๊ณ  ์žˆ์—ˆ๋‹ค. ์™„๋ฒฝ์€ ์•„๋‹ˆ์ง€๋งŒ ์˜†์˜ ๋™ํ–‰์ž์™€ ๋ฐœ๊ฑธ์Œ์„ ๋งž์ถ”๋ฉฐ ๊ฑธ์–ด๊ฐˆ ์ˆ˜ ์žˆ์„ ์ •๋„์˜€๋‹ค.

 

3) ์„ฑ์ทจ๊ฐ ์ฐพ๊ธฐ.

 ์‚ฌ๋žŒ์—๊ฒ ์›๋™๋ ฅ์ด ํ•„์š”ํ•˜๋—์ด ๊ณต๋ถ€ํ•˜๋Š” ์‚ฌ๋žŒ์—๊ฒ ๋ณด์—ฌ์ง€๋Š” ์„ฑ์ทจ๊ฐ์ด ํ•„์š”ํ•˜๋‹ค. ๋‚ด๊ฐ€ ์ž˜ ๋ฐฐ์šฐ๊ณ  ์žˆ๋Š”๊ฒŒ ๋งž๋‚˜, ๋‚ด๊ฐ€ ๋ฐฐ์šด๊ฒŒ ์ž˜ ์จ๋จน์–ด ์ง€๋Š”๊ฑด๊ฐ€, ๋‚ด๊ฐ€ ๋ฐฐ์šฐ๊ณ  ์ž˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์„๊นŒ ๋“ฑ๋“ฑ. ๊ทธ๋Ÿฐ ๊ฒƒ๋“ค์ด ์ •๋ง ์ง๊ด€์ ์œผ๋กœ ๋ณด์ด๊ณ  ๋Š๋‚„ ์ˆ˜ ์žˆ๋Š”๊ฒŒ ์ฝ”๋”ฉ์ธ ๊ฒƒ ๊ฐ™๋‹ค. ๋‹จ์ˆœํ•œ ๋‚˜์—๊ฒŒ๋Š” ์ด๋Ÿฐ ๊ณผ์ •์€ ๊ณ„์† ์ง‘์š”ํ•˜๊ฒŒ ๋งค๋‹ฌ๋ฆฌ๊ฒŒ ๋งŒ๋“ค์—ˆ๊ณ , ๊ฒฐ๊ตญ ์งง์€ ๊ธฐ๊ฐ„๋™์•ˆ 4์ฃผ์ฐจ๊นŒ์ง€ ๋‹ฌ๋ ค์˜ฌ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

 


 

 

์‚ฌํšŒ๋ณต์ง€๋ฅผ ์ „๊ณตํ•˜๊ณ  ์‚ฌ๋žŒ์„ ๋‹ฌ๋ž˜๋˜ ์‚ฌ๋žŒ์ด ์ด๋ฒˆ์—” ์ปดํ“จํ„ฐ๋ฅผ ๋‹ฌ๋ž˜๊ณ  ์žˆ๋‹ค.

 

๋‚˜์™€ ๊ฐ™์€ ์‚ฌ๋žŒ์€ ๋งŽ์„ ๊ฒƒ์ด๊ณ  ์•„๋งˆ ์žฆ์€ ๊ณ ๋น„๋ฅผ ๋งˆ์ฃผํ•  ๊ฒƒ์ด๋‹ค.

 

์ด๊ฒƒ์ด ๋‚ด๊ฐ€ ์ข‹์•„ํ•˜๋Š” ์ผ์ด ๋˜์—ˆ์œผ๋ฉด ์ข‹๊ฒ ๊ณ , ์ง€๊ธˆ ํ•˜๋Š” ์ผ์ด ๋ฌด์˜๋ฏธํ•˜์ง€ ์•Š์•˜์œผ๋ฉด ํ•œ๋‹ค.

 

์ž˜ ๋”ฐ๋ผ์™”๊ณ , ๋” ๋ถ™์ซ’๊ณ  ๊ฐ€๊ณ  ์‹ถ๋‹ค.

 

๋‹ค์Œ ๋ฒˆ์—๋Š” ํ•จ๊ป˜ ๊ฑธ์–ด๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์‚ฌ๋žŒ์ด ๋˜๊ณ  ์‹ถ๋‹ค.

 
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.