
도움 부탁드립니다.


from flask import Flask, render_template, request, jsonify
app = Flask(__name__)
from pymongo import MongoClient
client = MongoClient('mongodb+srv://test:sparta@cluster0.ptuqco9.mongodb.net/?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.naheedo.insert_one(doc)
return jsonify({'msg':'응원 고마워!'})
@app.route("/homework", methods=["GET"])
def homework_get():
comment_list = list(db.naheedo.find({},{'_id': False}))
return jsonify({'comments': comment_list})
if __name__ == '__main__':
app.run('0.0.0.0', port=5000, debug=True)
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
crossorigin="anonymous"></script>
<title>김태리 팬명록</title>
<link href="https://fonts.googleapis.com/css2?family=Gowun+Dodum&display=swap" rel="stylesheet">
<style>
* {
font-family: 'Gowun Dodum', sans-serif;
}
.mytitle {
width: 100%;
height: 300px;
background-image: linear-gradient(0deg, rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url("https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FcOTHEp%2FbtqVnXoJjPj%2Ft5NQ8STfV7YbHMYkKYNyyK%2Fimg.jpg");
background-position: center 30%;
background-size: cover;
color: white;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.mybox {
/*background-color: antiquewhite;*/
max-width: 500px;
width: 95%;
margin: 20px auto 10px auto;
box-shadow: 0px 0px 4px 0px gray;
padding: 20px;
}
.mybox > button {
margin-top: 10px;
}
.box{
max-width: 500px;
width: 95%;
margin: 20px auto 10px auto;
box-shadow: 0px 0px 4px 0px darkgray;
padding: 10px 5px 10px 5px;
}
.card {
max-width: 500px;
width: 95%;
margin: 11px auto 0px auto;
}
/* card가 여러 개이므로 .mycards class로 묶는 것이 좋음
.mycards > .card로 여백주기 ->클래스 이름이므로 점*/
#enc{margin-bottom: 250px;}
/* id 선택자 #id이름 -> 단일개체 속성부여*/
</style>
<script>
$(document).ready(function(){
set_temp()
show_comment()
});
function set_temp() {
$.ajax({
type: "GET",
url: "http://spartacodingclub.shop/sparta_api/weather/gimhae", //김해날씨 url
data: {},
success: function (response) {
// console.log(response);
let icon = response['icon']
let temp = response['temp']
// console.log(icon, temp)
$("#img-weather").attr("src",icon);
$("#temp").text(temp);
}
})
}
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()
}
})
}
function show_comment(){
$.ajax({
type: "GET",
url: "/homework",
data: {},
success: function (response) {
let rows = response['comments']
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>${comment}</p>
<footer class="blockquote-footer">${name}, <cite title="Source Title">${i+1}번째 응원</cite>
</footer>
</blockquote>
</div>
</div>`
$('#enc').append(temp_html)
}
}
});
}
</script>
</head>
<body>
<div class="mytitle">
<h1>김태리 팬명록</h1>
<p>
<img alt="오늘의 날씨" id="img-weather" width="30" src=""/>
현재기온: <span id="temp">김태리 예쁘</span>도
</p>
</div>
<div class="mybox">
<div class="form-floating mb-3">
<input type="email" class="form-control" id="name" placeholder="name@example.com">
<label for="name">닉네임</label>
</div>
<div class="form-floating">
<textarea class="form-control" placeholder="Leave a comment here" id="comment"
style="height: 100px"></textarea>
<label for="comment">응원댓글</label>
</div>
<button onclick="save_comment()" type="button" class="btn btn-dark">응원 남기기</button>
</div>
<div class="box">
<h2> 명대사</h2>
<div class="card">
<div class="card-body">
<blockquote class="blockquote mb-0">
<p>흠, 금메달만 못 땄네. 이제 따면 되지, 뭐!</p>
<footer class="blockquote-footer">나희도, <cite title="Source Title">스물다섯 스물하나</cite>
</footer>
</blockquote>
</div>
</div>
<div class="card">
<div class="card-body">
<blockquote class="blockquote mb-0">
<p>러브가 무엇이오?</p>
<footer class="blockquote-footer">고애신, <cite title="Source Title">미스터선샤인</cite>
</footer>
</blockquote>
</div>
</div>
<div class="card">
<div class="card-body">
<blockquote class="blockquote mb-0">
<p>아가씨는 제 애기씨세요.</p>
<footer class="blockquote-footer">타마코(숙희), <cite title="Source Title">아가씨</cite>
</footer>
</blockquote>
</div>
</div>
</div>
<div class="box" id="enc">
<h2> 응원해주셨어요</h2>
</div>
</body>
</html>
