상세 컨텐츠

본문 제목

생활코딩 + 진자2+ 데이터베이스 flask 게시판 완성

서문

by bumychoi 2024. 1. 16. 19:43

본문

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>리스트 페이지</title>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
    <style>
        table{
            width: 400px;
        }
    </style>

</head>

<body>
    <h1><a href="/">생성 수정 삭제 완료</a></h1>
    <table>
        <thead>
            <tr>
                <td>번 호</td>
                <td>제 목</td>
                <td>내 용</td>
                <td>저장시간</td>
            </tr>
            <hr>
        </thead>
        <tbody>
            {% for topic in topics %}
            <tr>
                <td>
                    <a href="{{url_for('read', idx=topic._id)}}">
                        {{loop.index}}
                    </a>
                </td>    
                <td>
                    <a href="{{url_for('read', idx=topic._id)}}">
                        {{topic.title}}
                    </a>
                </td>
                <td>
                    {{topic.body}}
                </td>
                <td>
                    {{topic.time}}
                </td>
            </tr>
            {% endfor %}
        </tbody>
       
    </table>
    <hr>
    <h2>환영합니다.</h2>
        HELLO, WEB
    <br>
    <a href="/create/"><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>
</head>
<body>
    <h1> 등록페이지 </h1>
    <table>
        <tbody>
            <form action="/create/" method="POST">
                <p><input type="text" name="title" placeholder="title"></p>
                <p><textarea name="body" placeholder="body"></textarea></p>
                <p><input type="submit" value="create"></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: 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)

'서문' 카테고리의 다른 글

최초의 홈페이지~~  (0) 2022.12.06
환영합니다!  (0) 2022.12.05

관련글 더보기