<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)
댓글