<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>수정하기</title>
<style>
p{
background-color: burlywood;
width: 300px;
align-items: center;
}
</style>
</head>
<body>
<h1> 수정하기 </h1>
<table>
<tbody>
<form action="/update/{{result.id}}" method="POST">
<!-- <p><input type="text" name="id" value="{{result.id}}"></p> -->
<p><input type="text" name="title" placeholder="title" value="{{result.title}}"></p>
<p><textarea name="body" placeholder="body">{{result.body}}</textarea></p>
<p><input type="submit" value="update"></p>
</form>
</tbody>
</table>
<a href="/"><button>홈</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>상세보기</title>
<style>
p {
background-color: aqua;
width: 300px;
align-items: center;
}
</style>
</head>
<body>
<h1> 상세내역 </h1>
<table>
<!-- <p>{{id}}</p> -->
<p>{{result.title}}</p>
<p>{{result.body}}</p>
<!-- <p>{{result.time}}</p> -->
</table>
<ul>
<li><a href="/"><button>홈</button></a></li>
<li><a href="/update/{{result.id}}">수정하기</a></li>
<li><form action="/delete/{{result.id}}" method="POST"><input type="submit" value="삭제"></form></li>
</ul>
<!-- 여가서 오류 조심 -->
<p></p>
<!-- <form action="/delete/{{id}}/" method="POST"><input type="submit" value="삭제"></form> -->
</body>
</html>
from flask import Flask, render_template, request, redirect, url_for, jsonify, abort
from bson.objectid import ObjectId
from datetime import datetime
from pymongo import MongoClient
client = MongoClient(
"
)
db = client.jinja
app = Flask(__name__)
@app.route("/", methods=["GET"])
def home():
topics = list(db.egoing.find({}))
return render_template("index.html", topics=topics)
@app.route("/create/", methods=["GET", "POST"])
def create():
if request.method == "GET":
return render_template("create.html")
elif request.method == "POST":
# global nextID
title = request.form["title"]
body = request.form["body"]
doc = {
"title": title,
"body": body,
}
# url = "/read/" + str(nextID) + "/"
x = db.egoing.insert_one(doc)
return redirect(url_for("read", idx=x.inserted_id))
@app.route("/read/<idx>")
def read(idx):
# idx = request.args.get("idx")
if idx is not None:
data = db.egoing.find_one({"_id": ObjectId(idx)})
if data is not None:
result = {
"id": data.get("_id"),
"title": data.get("title"),
"body": data.get("body"),
}
return render_template("read.html", result=result)
return abort(400)
@app.route("/update/<idx>/", methods=["GET","POST"])
def update(idx):
if request.method == "GET":
if id is not None:
data = db.egoing.find_one({"_id": ObjectId(idx)})
if data is not None:
result = {
"id": data.get("_id"),
"title": data.get("title"),
"body": data.get("body"),
}
return render_template("update.html", result=result)
elif request.method == "POST":
idx=ObjectId(idx)
title = request.form["title"]
body = request.form["body"]
db.egoing.update_one({'_id':idx},{'$set':{'title':title,'body':body}})
# url = "/read/"+{idx}
return redirect(url_for("read", idx=idx))
@app.route("/delete/<idx>/", methods=["POST"])
def delete(idx):
idx=ObjectId(idx)
db.egoing.delete_one({'_id':idx})
return redirect("/")
if __name__ == "__main__":
app.run(debug=True, port=5000)