๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
Flask ์์ํด๋ณด๊ธฐ _ MongoDB id ๊ฐ์ ธ์ค๊ธฐ OR template filter (3) ๋ณธ๋ฌธ
Flask ์์ํด๋ณด๊ธฐ _ MongoDB id ๊ฐ์ ธ์ค๊ธฐ OR template filter (3)
์ง์ง์ํ์นด 2022. 11. 2. 09:51221031 ์์ฑ
<๋ณธ ๋ธ๋ก๊ทธ๋ popawaw ๋์ ๋ธ๋ก๊ทธ๋ฅผ ์ฐธ๊ณ ํด์ ๊ณต๋ถํ๋ฉฐ ์์ฑํ์์ต๋๋ค :-)>
https://popawaw.tistory.com/5?category=893162
flask 3_๋ฐ์ดํฐ๋ฒ ์ด์ค ์์ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
๊ฒ์ํ ์์ ๊ธ์์ฑ์ผ๋ก ๋ค์ด์จ ๋ฐ์ดํฐ ์๋ํ id ๊ฐ์ ๊ฐ์ ธ์ค๊ธฐ ์ํด์ inserted_id ๋ฅผ ์ฌ์ฉํฉ๋๋ค id ๊ฐ์ด ์ ์ถ๋ ฅ๋๋๊ฑธ ํ์ธ ํ ์ ์์ต๋๋ค.
popawaw.tistory.com
https://popawaw.tistory.com/6?category=893162
flask 4_template filter ์ฌ์ฉํ๊ธฐ
ํ์ฌ ์๊ฐ ๊ฐ์ด timestamp ๋ก ์ ์ฅ๋ฉ๋๋ค ํ์ง๋ง ์ฌ์ฉ์๊ฐ ๋ณด๊ธฐ์ ๋ถํธ ํ๊ธฐ ๋๋ฌธ์ ์๊ฐ ๊ฐ์ ๋ณ๊ฒฝํด์ ๋ณด์ฌ์ฃผ๋ ค๊ณ ํฉ๋๋ค. html ์์ ํจ์์ฒ๋ผ ์ฌ์ฉํ๊ธฐ ์ํด์ templat_filter ๋ผ๋๊ฑธ ์ฌ์ฉํฉ๋๋ค ๊ทธ๋ฆฌ
popawaw.tistory.com
๐ ๋ฐ์ดํฐ์ ๋ํ id๊ฐ
- app.py
- ๊ธ์์ฑ์ผ๋ก ๋ค์ด์จ ๋ฐ์ดํฐ์ ๋ํ id๊ฐ ๊ฐ์ ธ์ค๊ธฐ ์ํด inserted_id ์ฌ์ฉ
from flask import Flask
from flask import request
from flask import render_template
# MongoDB ์ฐ๊ฒฐ
from flask_pymongo import PyMongo
from pymongo import MongoClient
app = Flask(__name__)
# app.config["MONGO_URI"] = "mongodb://localhost:27017/myflask" # ํ๋กํ ์ฝ๋ช
// ์ฃผ์:ํฌํธ/ DB name
client = MongoClient("mongodb+srv://gani:hilee5868@cluster0.h7jyl5v.mongodb.net/?retryWrites=true&w=majority")
# mongo = PyMongo(app)
# ์ฃผ์ ์ค์
@app.route("/write", methods = ["GET", "POST"])
def board_write() :
# method๊ฐ POST ์ธ์ง GET์ธ์ง ๊ตฌ๋ถ
if request.method == "POST" :
# Template์ write.html๋ก ๋ณด์ฌ์ค๋ค
name = request.form.get("name")
title = request.form.get("title")
contents = request.form.get("contents")
# board ๋ผ๋ ์ปฌ๋ ์
์ ์ ๊ทผ ์์ผ๋ฉด ์ ๊ทผ, ์์ผ๋ฉด ์์ฑ
# board = mongo.db.board
db = client.myflask
post = {
"name" : name,
"title" : title,
"contents" : contents
}
data = db.myflask.insert_one(post)
# ๊ธ์์ฑ์ผ๋ก ๋ค์ด์จ ๋ฐ์ดํฐ์ ๋ํ id๊ฐ ๊ฐ์ ธ์ค๊ธฐ ์ํด inserted_id ์ฌ์ฉ
return str(data.inserted_id)
else :
return render_template("write.html")
if __name__ == "__main__" :
app.run(host = "0.0.0.0", debug = True, port = 8000)
๐ Template filter
- app.py
- ์๋จ
- ์ค๋ช ๋ ๋ถ์กฑํด์ ๋ชปํใ ใฑใ ์ใ
# 2) template filter ์ฌ์ฉ
from flask import Flask
from flask import request
from flask import render_template
# MongoDB ์ฐ๊ฒฐ
from flask_pymongo import PyMongo
from pymongo import MongoClient
from datetime import datetime
import time
app = Flask(__name__)
# app.config["MONGO_URI"] = "mongodb://localhost:27017/myflask" # ํ๋กํ ์ฝ๋ช
// ์ฃผ์:ํฌํธ/ DB name
client = MongoClient("mongodb+srv://gani:hilee5868@cluster0.h7jyl5v.mongodb.net/?retryWrites=true&w=majority")
# mongo = PyMongo(app)
# ์ฃผ์ ์ค์
@app.route("/write", methods = ["GET", "POST"])
def board_write() :
# method๊ฐ POST ์ธ์ง GET์ธ์ง ๊ตฌ๋ถ
if request.method == "POST" :
# Template์ write.html๋ก ๋ณด์ฌ์ค๋ค
name = request.form.get("name")
title = request.form.get("title")
contents = request.form.get("contents")
# timestape๋ ๋ฐ๋ฆฌ์ธ์ปจ๋๋ก ๊ฐ ๋ฐํ. ์ด๋ก ๋ณํํ๊ธฐ ์ํด 1000 ๊ณฑํ๊ธฐ
# ์์์ ์ด ๋์ค๋ ๊ฑธ ๋ฐฉ์งํ๊ธฐ ์ํด round ๋ฐํ
current_utc_time = round(datetime.utcnow().timestamp() * 1000)
# board ๋ผ๋ ์ปฌ๋ ์
์ ์ ๊ทผ ์์ผ๋ฉด ์ ๊ทผ, ์์ผ๋ฉด ์์ฑ
# board = mongo.db.board
db = client.myflask
post = {
"name" : name,
"title" : title,
"contents" : contents,
"pubdate" : current_utc_time,
"view" : 0
}
data = db.myflask.insert_one(post)
# ๊ธ์์ฑ์ผ๋ก ๋ค์ด์จ ๋ฐ์ดํฐ์ ๋ํ id๊ฐ ๊ฐ์ ธ์ค๊ธฐ ์ํด inserted_id ์ฌ์ฉ
return str(data.inserted_id)
else :
return render_template("write.html")
# html ์์ ํจ์์ฒ๋ผ ์ฌ์ฉํ๊ธฐ ์ํด templat_filter ์ฌ์ฉ
@app.template_filter("format_time")
def format_time(value) :
if value is None :
return ""
# ํด๋ผ์ด์ธํธ ํ์ฌ ์๊ฐ
now_timestamp = time.time()
offset = datetime.fromtimestamp(now_timestamp) - datetime.utcfromtimestamp(now_timestamp)
value = datetime.fromtimestamp((int(value) / 1000) + offset)
return value.strftime("%Y-%m-%d %H:%M:%S")
if __name__ == "__main__" :
app.run(host = "0.0.0.0", debug = True, port = 8000)
'๐ฉโ๐ป ๋ฐฑ์๋(Back-End) > Node js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ค์ ๋์ .. Flask ๊ธฐ๋ณธ์ ์ธ ์น ํ์ด์ง ๊ตฌ์ถ (1) (0) | 2022.11.15 |
---|---|
Flask ์น ํ์ด์ง ๋ง๋ค๊ธฐ with Best Book to Read in 2021 ํ์์ ๋ฐ์ดํฐ ๋ถ์ (0) | 2022.11.11 |
Flask ์์ํด๋ณด๊ธฐ _ MongoDB ์ฐ๊ฒฐํ๊ธฐ (2) (0) | 2022.10.31 |
Flask ์์ํด๋ณด๊ธฐ _ ๊ฒ์๊ธ ๋ง๋ค๊ธฐ (1) (0) | 2022.10.31 |
[v0.5]Django_ํฌ๋กค๋งํ ๋ฐ์ดํฐ ์ ์ฅํ๊ธฐ (0) | 2022.01.29 |