
* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.
* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.
화성땅 주문하기 버튼을 눌러도 저장완료! 가 안뜨고 파이몽고에도 데이터가 들어가지 않아요
작성한 코드 및 에러 메세지
from flask import Flask, render_template, request, jsonify
app = Flask(__name__)
from pymongo import MongoClient
import certifi
ca = certifi.where()
client = MongoClient('mongodb+srv://sparta:<password>@cluster0.d4m39ds.mongodb.net/?retryWrites=true&w=majority', tlsCAFile=ca)
db = client.dbsparta
@app.route('/')
def home():
return render_template('index.html')
@app.route("/mars", methods=["POST"])
def mars_post():
name_receive = request.form['name_give']
address_receive = request.form['address_give']
size_receive = request.form['size_give']
doc = {
'name' : name_receive,
'address' : address_receive,
'size' : size_receive
}
db.mars.insert_one(doc)
return jsonify({'msg':'저장완료!'})
@app.route("/mars", methods=["GET"])
def mars_get():
return jsonify({'msg':'GET 연결 완료!'})
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>
<link href="https://fonts.googleapis.com/css2?family=Gowun+Batang:wght@400;700&display=swap" rel="stylesheet" />
<title>선착순 공동구매</title>
<style>
* {
font-family: "Gowun Batang", serif;
color: white;
}
body {
background-image: linear-gradient(0deg,
rgba(0, 0, 0, 0.5),
rgba(0, 0, 0, 0.5)),
url("https://cdn.aitimes.com/news/photo/202010/132592_129694_3139.jpg");
background-position: center;
background-size: cover;
}
h1 {
font-weight: bold;
}
.order {
width: 500px;
margin: 60px auto 0px auto;
padding-bottom: 60px;
}
.mybtn {
width: 100%;
}
.order>table {
margin: 40px 0;
font-size: 18px;
}
option {
color: black;
}
</style>
<script>
$(document).ready(function () {
show_order();
});
function show_order() {
fetch('/mars').then((res) => res.json()).then((data) => {
console.log(data)
alert(data['msg'])
})
}
function save_order() {
let name = $('#name').val()
let address = $('#address').val()
let size = $('#size').val()
let formData = new FormData();
formData.append("name_give", name);
formData.append("address_give", address);
formData.append("size_give", size);
fetch('/mars', { method: "POST", body: formData }).then((res) => res.json()).then((data) => {
alert(data["msg"]);
});
}
</script>
</head>
<body>
<div class="mask"></div>
<div class="order">
<h1>화성에 땅 사놓기!</h1>
<h3>가격: 평 당 500원</h3>
<p>
화성에 땅을 사둘 수 있다고?<br />
앞으로 백년 간 오지 않을 기회. 화성에서 즐기는 노후!
</p>
<div class="order-info">
<div class="input-group mb-3">
<span class="input-group-text">이름</span>
<input id="name" type="text" class="form-control" />
</div>
<div class="input-group mb-3">
<span class="input-group-text">주소</span>
<input id="address" type="text" class="form-control" />
</div>
<div class="input-group mb-3">
<label class="input-group-text" for="size">평수</label>
<select class="form-select" id="size">
<option selected>-- 주문 평수 --</option>
<option value="10평">10평</option>
<option value="20평">20평</option>
<option value="30평">30평</option>
<option value="40평">40평</option>
<option value="50평">50평</option>
</select>
</div>
<button onclick="save_order()" type="button" class="btn btn-warning mybtn">
주문하기
</button>
</div>
<table class="table">
<thead>
<tr>
<th scope="col">이름</th>
<th scope="col">주소</th>
<th scope="col">평수</th>
</tr>
</thead>
<tbody id="order-box">
<tr>
<td>홍길동</td>
<td>서울시 용산구</td>
<td>20평</td>
</tr>
<tr>
<td>임꺽정</td>
<td>부산시 동구</td>
<td>10평</td>
</tr>
<tr>
<td>세종대왕</td>
<td>세종시 대왕구</td>
<td>30평</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
admin@DESKTOP-NCVPPAL MINGW64 ~/Desktop/이승우/sparta/projects/02.mars
$ source c:/Users/admin/Desktop/이승우/sparta/projects/02.mars/venv/Scripts/activate
(venv)
admin@DESKTOP-NCVPPAL MINGW64 ~/Desktop/이승우/sparta/projects/02.mars$ c:/Users/admin/Desktop/이승우/sparta/projects/02.mars/venv/Scripts/python.exe c:/Users/admin/Desktop/이승우/sparta/projects/02.mars/app.py
* Serving Flask app 'app'
* Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on all addresses (0.0.0.0)
* Running on http://127.0.0.1:5000
* Running on http://192.168.123.102:5000
Press CTRL+C to quit
* Restarting with stat
* Debugger is active!
* Debugger PIN: 140-369-706
127.0.0.1 - - [19/Feb/2023 21:10:13] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [19/Feb/2023 21:10:13] "GET /mars HTTP/1.1" 200 -
127.0.0.1 - - [19/Feb/2023 21:10:26] "POST /mars HTTP/1.1" 500 -
Traceback (most recent call last):
File "C:\Users\admin\Desktop\이승우\sparta\projects\02.mars\venv\lib\site-packages\flask\app.py", line 2551, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\admin\Desktop\이승우\sparta\projects\02.mars\venv\lib\site-packages\flask\app.py", line 2531, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\admin\Desktop\이승우\sparta\projects\02.mars\venv\lib\site-packages\flask\app.py", line 2528, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\admin\Desktop\이승우\sparta\projects\02.mars\venv\lib\site-packages\flask\app.py", line 1825, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\admin\Desktop\이승우\sparta\projects\02.mars\venv\lib\site-packages\flask\app.py", line 1823, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\admin\Desktop\이승우\sparta\projects\02.mars\venv\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
File "c:\Users\admin\Desktop\이승우\sparta\projects\02.mars\app.py", line 25, in mars_post
db.mars.insert_one(doc)
File "C:\Users\admin\Desktop\이승우\sparta\projects\02.mars\venv\lib\site-packages\pymongo\collection.py", line 628, in insert_one
self._insert_one(
File "C:\Users\admin\Desktop\이승우\sparta\projects\02.mars\venv\lib\site-packages\pymongo\collection.py", line 569, in _insert_one
self.__database.client._retryable_write(acknowledged, _insert_command, session)
File "C:\Users\admin\Desktop\이승우\sparta\projects\02.mars\venv\lib\site-packages\pymongo\mongo_client.py", line 1476, in _retryable_write
return self._retry_with_session(retryable, func, s, None)
File "C:\Users\admin\Desktop\이승우\sparta\projects\02.mars\venv\lib\site-packages\pymongo\mongo_client.py", line 1349, in _retry_with_session
return self._retry_internal(retryable, func, session, bulk)
File "C:\Users\admin\Desktop\이승우\sparta\projects\02.mars\venv\lib\site-packages\pymongo\_csot.py", line 105, in csot_wrapper
return func(self, *args, **kwargs)
File "C:\Users\admin\Desktop\이승우\sparta\projects\02.mars\venv\lib\site-packages\pymongo\mongo_client.py", line 1381, in _retry_internal with self._get_socket(server, session) as sock_info:
File "C:\Users\admin\AppData\Local\Programs\Python\Python38\lib\contextlib.py", line 113, in __enter__
return next(self.gen)
File "C:\Users\admin\Desktop\이승우\sparta\projects\02.mars\venv\lib\site-packages\pymongo\mongo_client.py", line 1217, in _get_socket
with server.get_socket(handler=err_handler) as sock_info:
File "C:\Users\admin\AppData\Local\Programs\Python\Python38\lib\contextlib.py", line 113, in __enter__
return next(self.gen)
File "C:\Users\admin\Desktop\이승우\sparta\projects\02.mars\venv\lib\site-packages\pymongo\pool.py", line 1407, in get_socket
sock_info = self._get_socket(handler=handler)
File "C:\Users\admin\Desktop\이승우\sparta\projects\02.mars\venv\lib\site-packages\pymongo\pool.py", line 1520, in _get_socket
sock_info = self.connect(handler=handler)
File "C:\Users\admin\Desktop\이승우\sparta\projects\02.mars\venv\lib\site-packages\pymongo\pool.py", line 1378, in connect
sock_info.authenticate()
File "C:\Users\admin\Desktop\이승우\sparta\projects\02.mars\venv\lib\site-packages\pymongo\pool.py", line 870, in authenticate
auth.authenticate(creds, self)
File "C:\Users\admin\Desktop\이승우\sparta\projects\02.mars\venv\lib\site-packages\pymongo\auth.py", line 549, in authenticate
auth_func(credentials, sock_info)
File "C:\Users\admin\Desktop\이승우\sparta\projects\02.mars\venv\lib\site-packages\pymongo\auth.py", line 475, in _authenticate_default
return _authenticate_scram(credentials, sock_info, "SCRAM-SHA-1")
File "C:\Users\admin\Desktop\이승우\sparta\projects\02.mars\venv\lib\site-packages\pymongo\auth.py", line 241, in _authenticate_scram
res = sock_info.command(source, cmd)
File "C:\Users\admin\Desktop\이승우\sparta\projects\02.mars\venv\lib\site-packages\pymongo\pool.py", line 767, in command
return command(
File "C:\Users\admin\Desktop\이승우\sparta\projects\02.mars\venv\lib\site-packages\pymongo\network.py", line 166, in command
helpers._check_command_response(
File "C:\Users\admin\Desktop\이승우\sparta\projects\02.mars\venv\lib\site-packages\pymongo\helpers.py", line 181, in _check_command_response
raise OperationFailure(errmsg, code, response, max_wire_version)
pymongo.errors.OperationFailure: bad auth : authentication failed, full error: {'ok': 0, 'errmsg': 'bad auth : authentication failed', 'code': 8000, 'codeName': 'AtlasError'}
