커뮤니티
포인트
쿠폰
내 강의실
국비 신청 내역
증명서
계정
로그아웃
학습 질문
개발 일지
나의 활동
답변 완료
mongoDB 에러
undefined주차
북마크
정*기
댓글
15
추천
0
조회수
44
조회수
44
답변 완료
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>1주차 프로젝트</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"
      integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

    <style>
        .title {
            height: 200px;
            background-color: cornflowerblue;
            background-image: url("");
            background-position: center;
            background-size: auto;

            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
        }

        .img_div {
            /*background-color: blueviolet;*/
            text-align: center;
        }

        .img_div > img {
            width: 500px;
            height: 400px
            /*margin-top: 100px; 왜 안되지?*/
        }

        .postbox {
            width: 95%;
            max-width: 500px;
            margin: 20px auto 0px auto;
            padding: 20px;
            box-shadow: 0px 0px 3px 0px gray;

            display: none;
        }

        .btns {
            display: flex;
            flex-direction: row;
            justify-content: center;
            align-items: center;

            margin-top: 20px;
        }

        .btns > btn {
            margin: 20px;
        }
    </style>

    <script>
        // function savebtn() {
        //     alert('저장되었습니다');
        // }
        // function cancelbtn() {
        //     alert('취소되었습니다')
        // }

        $(document).ready(function () {
            show_commentList();
        });

        function open_box(){
            $('#postbox').show()
        }

        function savebtn() {
            let comment = $('#comment').val()

            $.ajax({
                type: "POST",
                url: "/project01",
                data: {'comment_give' : comment},
                success: function (response) {
                    alert(response["msg"])
                    window.location.reload()
                }
            });
        }

        function show_commentList() {
            $.ajax({
                type: 'GET',
                url: '/project01',
                data: {},
                success: function (response) {
                    let rows = response['comment']
                    for (let i = 0; i < rows.length; i++) {
                        let comment = rows[i]['comment']

                        console.log(response)

                        let temp_html = `<div class="input-group">
                                            <span class="input-group-text">닉네임</span>
                                            <textarea class="form-control" aria-label="With textarea">${comment}</textarea>
                                        </div>`
                        $('#list-box').append(temp_html)
                    }
                }
            });
        }
    </script>
</head>

<body>
<div class="title">
    <img>
    <h1></h1>
    <a href="/detail" class="">상세페이지로 이동</a>
    </img>

</div>
<button onclick="open_box()" type="button" class="roginbtn btn-warning">로그인</button>
<div class="img_div">
    <img src="https://www.lottehotel.com/content/dam/lotte-hotel/lotte/busan/facilities/fitness-club/gymnasium/180829-2-2000-fac-busan-hotel.jpg.thumb.1920.1920.jpg" alt="">
</div>

<div class="postbox" id="postbox">
    <div class="form-floating">
        <textarea class="form-control" placeholder="Leave a comment here" id="floatingTextarea"
                  style="height: 100px"></textarea>
        <label for="floatingTextarea">후기를 입력해 주세요.</label>
    </div>

    <div class="btns">
        <button onclick="savebtn()" type="button" class="btn btn-light">저장</button>
        <button onclick="cancelbtn()" type="button" class="btn btn-dark">취소</button>
    </div>
</div>

<div id="list-box">
    <p> </p>
</div>

</body>
</html>

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

from pymongo import MongoClient
client = MongoClient('mongodb+srv://test1:sparta@cluster0.5ju0bxd.mongodb.net/?retryWrites=true&w=majority')
db = client.dbsparta

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

@app.route('/detail')
def detail():
    return render_template("detail.html")

@app.route("/project01", methods=["POST"])
def comment_post():
    comment_receive = request.form['comment_give']

    doc = {
        'comment': comment_receive
    }
    db.project01.insert_one(doc)

    return jsonify({'msg': '후기 등록 완료!'})

@app.route("/project01", methods=["GET"])
def comment_get():
    comment_list = list(db.project01.find({}, {'_id': False}))
    return jsonify({'comment': comment_list})



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

어제는 웹페이지에서 코멘트 남긴 게 몽고디비로 잘 넘어갔는데 오늘은 안됩니다..!

어떤 문제인지 확인해주실 수 있으실까요..?

취소
 공유
취소
댓글 0
댓글 알림
나의얼굴