from flask import Flask, render_template, request, url_for, abort, redirect, redirect
from bson.objectid import ObjectId
from pymongo import MongoClient
client = MongoClient(")
db = client.d_bar
app = Flask(__name__)
@app.route("/", methods=["GET"])
def home():
mv_list = list(db.mv.find({}).sort({"date": -1}))
return render_template("home.html", lists=mv_list)
@app.route("/mv_create/", methods=["GET", "POST"])
def mv_create():
if request.method == "GET":
return render_template("mv_create.html")
elif request.method == "POST":
date = request.form["inport_date"]
mv_name = request.form["mv_name"]
port_name = request.form["port_name"]
remark = request.form["remark"]
doc = {
"date": date,
"mv_name": mv_name,
"port_name": port_name,
"remark": remark,
}
x = db.mv.insert_one(doc)
return redirect(url_for("read", idx=x.inserted_id))
@app.route("/read/<idx>")
def read(idx):
if idx is not None:
data = db.mv.find_one({"_id": ObjectId(idx)})
if data is not None:
result = {
"id": data.get("_id"),
"date": data.get("date"),
"mv_name": data.get("mv_name"),
"port_name": data.get("port_name"),
"remark": data.get("remark"),
}
return render_template("read.html", result=result)
return abort(404)
@app.route("/update/<idx>", methods=["GET", "POST"])
def update(idx):
if request.method == "GET":
if id is not None:
data = db.mv.find_one({"_id": ObjectId(idx)})
if data is not None:
result = {
"id": data.get("_id"),
"date": data.get("date"),
"mv_name": data.get("mv_name"),
"port_name": data.get("port_name"),
"remark": data.get("remark"),
}
return render_template("update.html", result=result)
elif request.method == "POST":
idx = ObjectId(idx)
date = request.form["inport_date"]
mv_name = request.form["mv_name"]
port_name = request.form["port_name"]
remark = request.form["remark"]
db.mv.update_one({"_id":idx},{'$set':{"date":date,
'mv_name':mv_name,
'port_name':port_name,
'remark':remark}})
return redirect(url_for("read",idx=idx))
@app.route("/delete/<idx>/", methods=["POST"])
def delete(idx):
idx=ObjectId(idx)
db.mv.delete_one({'_id':idx})
return redirect("/")
if __name__ == "__main__":
app.run(debug=True, port=5001)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous">
integrity="sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm"
crossorigin="anonymous"></script>
</head>
<body>
<h1>상세내역</h1>
<table class="table" style="border: 8px blue;" >
<tbody>
<tr>
<th>입항일</th>
<td>{{result.date}}</td>
</tr>
<tr>
<th>본선명</th>
<td>{{result.mv_name}}</td>
</tr>
<tr>
<th>하역사</th>
<td>{{result.port_name}}</td>
</tr>
<tr>
<th>비 고</th>
<td>{{result.remark}}</td>
</tr>
</tbody>
</table>
<a href="/"><button type="button" class="btn btn-primary">홈</button></a>
<a href="/update/{{result.id}}"><button type="button" class="btn btn-outline-danger">수정하기</button></a>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous">
integrity="sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm"
crossorigin="anonymous"></script>
</head>
<body>
<h1>상세내역</h1>
<form action="/update/{{result.id}}" method="POST">
<p><input class="form-control form-control-lg" type="date" name="inport_date" placeholder="입항일" value="{{result.date}}"></p>
<p><input class="form-control" type="text" type="text" name="mv_name" placeholder="본선명" value="{{result.mv_name}}"></p>
<p><input class="form-control" type="text" type="text" name="port_name" placeholder="하역사" value="{{result.port_name}}"></p>
<div class="form-floating"><textarea textarea class="form-control" placeholder="비 고" id="floatingTextarea2" style="height: 200px" name="remark">{{result.remark}}</textarea>
</div>
<input type="submit" class="btn btn-danger" value="수 정">
</form>
</table>
<br>
<hr>
<div class="d-grid gap-2 d-md-flex justify-content-md-end">
<a href="/"><button class="btn btn-primary" type="button" class="btn btn-primary">홈</button></a>
<form action="/delete/{{result.id}}" method="POST"><input type="submit" class="btn btn-danger" value="삭제"></form>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous">
integrity="sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm"
crossorigin="anonymous"></script>
</head>
<body>
<h1>본선 등록</h1>
<form action="/mv_create/" method="POST">
<p><input class="form-control form-control-lg" type="date" name="inport_date" placeholder="입항일"></p>
<p><input class="form-control" type="text" type="text" name="mv_name" placeholder="본선명"></p>
<p><input class="form-control" type="text" type="text" name="port_name" placeholder="하역사"></p>
<div class="form-floating"><textarea textarea class="form-control" placeholder="비 고" id="floatingTextarea2" style="height: 200px"></textarea>
<label for="floatingTextarea2">전달사항</label>
</div>
<input type="submit" value="등록">
</form>
<hr>
<br>
<a href="/"><button type="button" class="btn btn-primary">홈</button></a>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous">
integrity="sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm"
crossorigin="anonymous"></script>
</head>
<body>
<nav class="navbar navbar-expand-lg bg-body-tertiary">
<div class="container-fluid">
<a class="navbar-brand" href="/">철근홈</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="/mv_create/">본선등록</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">본선내역관리</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">화주관리</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" aria-disabled="true">화종관리</a>
</li>
</ul>
</div>
</div>
</nav>
<table class="table table-striped">
<thead>
<tr>
<td>순 번</td>
<td>입 항 일</td>
<td>본 선 명</td>
<td>하 역 사</td>
<td>비 고</td>
</tr>
<hr>
</thead>
<tbody>
{% for list in lists %}
<tr>
<td>
<a href="{{url_for('read',idx=list._id)}}">
{{loop.index}}
</a>
</td>
<td>
<a href="{{url_for('read',idx=list._id)}}">
{{list.date}}
</a>
</td>
<td>
<a href="{{url_for('read',idx=list._id)}}">
{{list.mv_name}}
</a>
</td>
<td>{{list.port_name}}</td>
<td>{{list.remark}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>