파이썬과 친해지기

J.H.H.·2021년 9월 14일
0
post-thumbnail

아직 친해지기 좀 힘들다. 새벽 2시 37분 너무 졸립지만 그래도 끝까지!!!간다!!!
스파르타 웹개발 종합반 4주차 과제!


서버

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('homework_02week_junhee_Han.html')

주문하기(POST) API
@app.route('/order', methods=['POST'])
def save_order():
name_receive = request.form['name_give']
quantity_receive = request.form['quantity_give']
address_receive = request.form['address_give']
phone_receive = request.form['phone_give']

doc = {
    'name':name_receive,
    'quantity':quantity_receive,
    'address':address_receive,
    'phone':phone_receive
}
db.chuseokoder.insert_one(doc)
return jsonify({'result': 'success', 'msg': '해당주문이 정상적으로 등록됐습니다!'})

주문 목록보기(Read) API
@app.route('/order', methods=['GET'])
def view_orders():
orders = list(db.chuseokoder.find({},{'_id':False}))

return jsonify({'result': 'success', 'orders': orders})

if name == 'main':
app.run('0.0.0.0', port=5000, debug=True)


클라이언트 html 스크립트

    $(document).ready(function () {
      get_rate();
      listing();
    })

     function listing() {
        $.ajax({
            type: "GET",
            url: "/order",
            data: {},
            success: function (response) {
                if (response["result"] == "success") {
                    let orders = response['orders'];
                    for (let i = 0; i < orders.length;i++) {
                        let name = orders[i]['name']
                        let quantity = orders[i]['quantity']
                        let address = orders[i]['address']
                        let phone = orders[i]['phone']

                        let temp_html = `<tr>
                                             <th scope="row">${name}</th>
                                             <td>${quantity}</td>
                                             <td>${address}</td>
                                             <td>${phone}</td>
                                         </tr>`
                        $('#orders-box').append(temp_html)
                    }
                }
            }
        })
    }


    function order() {
        let name = $('#order-name').val()
        let quantity = $('#order-quantity').val()
        let address = $('#order-address').val()
        let phone = $('#order-phone').val()

        $.ajax({
            type: "POST",
            url: "/order",
            data: {name_give: name, quantity_give: quantity, address_give: address, phone_give: phone},
            success: function (response) {
                alert(response["msg"]);
                window.location.reload();
            }
        })
    }


    function get_rate() {
        $.ajax({
            type: "GET",
            url: "http://spartacodingclub.shop/sparta_api/rate",
            data: {},
            success: function (response) {
                let wondallar = response['rate']
                $('#wondallor').text(wondallar)
            }
        })
    }

스크립트 끝

profile
코린이를 넘어 데이터 시각화를 휘두르는(?) 그날까지

0개의 댓글