2/15作業

1112 ワード

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']
address_receive = request.form['address_give']
phone_receive = request.form['phone_give']
doc = {
    'name': name_receive,
    'count': count_receive,
    'address': address_receive,
    'phone': phone_receive
}
db.orders.insert_one(doc)

return jsonify({'result': 'success', 'msg': '주문 완료!'})
受注リスト(Read)APIの表示
@app.route('/order', methods=['GET'])
def view_orders():
orders = list(db.orders.find({}, {'_id': False}))
return jsonify({'result': 'success', 'orders': orders})
if name == 'main':
app.run('0.0.0.0', port=5000, debug=True)