Python 모듈 Flask, ArangoDB 및 CSS 프레임 워크 Bulma를 사용하는 여러 HTML 파일을 사용하여 노트 작성 웹 사이트를 만들고 싶습니다. 잘 진행되고 있지만 배열의 모든 요소를 HTML로 표시하는 방법을 모르겠습니다. 스크립트 태그를 사용해야한다고 생각하는데 사용 방법을 모르겠습니다. 저는 JavaScript조차 모릅니다. 어쨌든, 이것에 도움을 줄 사람이 있습니까? 내 파이썬 코드는 다음과 같습니다.
from flask import Flask, render_template, request, redirect, url_for
from arango import ArangoClient
client = ArangoClient()
db = client.db("notes", username="root", password="************")
users = db.collection("Users")["Users"]["users"]
failed = ""
failed_message = "This username is taken"
username = ""
welcome = ""
new_note_message = "Create a new note!"
app = Flask(__name__)
def check_usernames(new_username):
users = db.collection("Users")["Users"]["users"]
for i in users:
if i["username"] == new_username:
return False
return True
def check_notes(new_note):
notes = db.collection("Users")[username]["notes"]
for i in notes:
if i["title"] == new_note:
return False
return True
@app.route("/")
def home():
global username
backup = username
username = ""
global failed
failed = ""
if backup == "":
return render_template("index.html")
else:
username = backup
return redirect(url_for("notes_page"))
@app.route("/after-login", methods=["POST"])
def handle_login():
global welcome
welcome = "Welcome back!"
users = db.collection("Users")["Users"]["users"]
if {"username": request.form["Username"], "password": request.form["Password"]} in users:
global username
username = request.form["Username"]
return redirect(url_for("notes_page"))
else:
global failed
failed = " failed"
return redirect(url_for("login"))
@app.route("/logout")
def handle_logout():
global username
username = ""
return redirect(url_for("home"))
@app.route("/login")
def login():
if username == "":
return render_template("login.html", message="Login" + failed)
else:
return redirect(url_for("notes_page"))
@app.route("/register")
def register():
if username == "":
return render_template("register.html", message="Register" + failed, submessage="")
else:
return redirect(url_for("notes_page"))
@app.route("/after-register", methods=["POST"])
def handle_register():
global welcome
welcome = "Welcome!"
users = db.collection("Users")["Users"]["users"]
if check_usernames(request.form["Username"]):
global username
username = request.form["Username"]
users.append({"username": request.form["Username"], "password": request.form["Password"]})
db.aql.execute("UPDATE {_key: 'Users'} WITH {users: " + str(users) + "} INTO Users")
db.aql.execute("INSERT {_key: '" + username + "'} INTO Users")
db.aql.execute("UPDATE {_key: '" + username + "'} WITH {notes: []} INTO Users")
return redirect(url_for("notes_page"))
else:
global failed
failed = " failed"
return redirect(url_for("register"))
@app.route("/notes")
def notes_page():
global notes
notes = db.collection("Users")[username]["notes"]
if username != "":
return render_template("notes.html", username=username, notes=notes, welcome=welcome)
else:
return redirect(url_for("home"))
@app.route("/new")
def new():
return render_template("create.html", message=new_note_message)
@app.route("/after-note", methods=["POST"])
def handle_new_note():
if check_notes(request.form["Title"]):
global notes
notes = db.collection("Users")[username]["notes"]
notes.append({"title": request.form["Title"], "note": request.form["Note"]})
db.aql.execute("UPDATE {_key: '" + username + "'} WITH {notes: " + str(notes) + "} INTO Users")
return redirect(url_for("notes_page"))
else:
global new_note_message
new_note_message = "You already made a note with this title"
return redirect(url_for("new"))
if __name__ == "__main__":
app.run(debug=True)
특정 페이지에 대한 내 HTML 코드 :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{welcome}}</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css">
</head>
<body>
<section class="section has-text-centered">
<h1 class="title is-2">Hi {{username}}!</h1>
<a href={{url_for('new')}} class="button is-rounded is-outlined is-primary">Create a note</a>
<a href={{url_for('handle_logout')}} class="button is-rounded is-outlined is-danger">Log out</a>
</section>
</body>
</html>
사용자가 만든 메모를 2 개의 버튼 아래에두고, 그렇지 않은 경우 프로그램에서 "메모가 없습니다. 오늘 첫 번째 메모를 만드세요!"라고 말하도록하고 싶습니다.
이것을 할 방법이 있습니까?
감사
배열을 HTML 파일에 전달하고 Jinja2를 사용하여 요소를 반복 할 수 있습니다.
{% for item in array %}
<!--- what you want to loop --->
{% endfor %}
더 많은 정보가 필요하면 문서를 읽으십시오 : https://jinja.palletsprojects.com/en/2.11.x/templates/
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다