본문 바로가기
front

숙제해석

by 리승우 2022. 8. 8.
<script>
    $(document).ready(function () {
        show_comment();
    });

    function show_comment() {
        $.ajax({
            type: "GET",
            url: "/homework",
            data: {},
            success: function (response) {
                let rows = response['fans']
                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>${name}</p>
                <footer class="blockquote-footer">${comment}</footer>
            </blockquote>
        </div>
    </div>`
                $('#comment-list').append(temp_html)

                }
                }
            }
        );
    }

    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()
            }
        });
    }

</script>
from flask import Flask, render_template, request, jsonify
app = Flask(__name__)

from pymongo import MongoClient
client = MongoClient('mongodb+srv://text:sparta@cluster0.44vwqnl.mongodb.net/Cluster0?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.pan.insert_one(doc)
    return jsonify({'msg':'응원 완료!'})

@app.route("/homework", methods=["GET"])
def homework_get():
    pan_list = list(db.pan.find({}, {'_id': False}))
    return jsonify({'fans':pan_list})


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

'front' 카테고리의 다른 글

웹개발 프로그래밍 작업과정  (0) 2022.08.23
버킷리스트  (0) 2022.08.16
화성땅 공동구매 (mongodb, jquery, ajax, api)  (0) 2022.08.07
GET POST 방식  (0) 2022.08.04
Flask  (0) 2022.08.04

댓글