본문 바로가기
Study

웹 개발 4주차 숙제

by 186kg 2021. 9. 10.

Mongodb, Pymongo, Robo3T, Flask를 활용하여 서버 만들기 연습

from flask import Flask, render_template, jsonify, request
app = Flask(__name__)

from pymongo import MongoClient
client = MongoClient('localhost', 27017)
db = client.dbsparta

@app.route('/')
def home():
    return render_template('index.html') 

@app.route('/order', methods=['POST'])
def order():
    name_receive = request.form['name_give']
    select_receive = request.form['select_give']
    phone_receive = request.form['phone_give']
    address_receive = request.form['address_give']
    time_receive = request.form['time_give']

    doc = {
        'time': time_receive,
        'name': name_receive,
        'select': select_receive,
        'phone': phone_receive,
        'address': address_receive
    }

    db.orders.insert_one(doc)

    return jsonify({'message': '주문되었습니다.'})

@app.route('/order', methods=['GET'])
def listing():
    orders = list(db.orders.find({},{'_id': False}))
    return jsonify({'all_orders': orders})

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

댓글