有没有办法显示HTML中python数组中的所有元素?

切尔迪平基

我想使用python模块Flask,ArangoDB和一堆带有CSS框架Bulma的HTML文件创建一个记笔记的网站。进行得很好,但是我不知道如何以HTML显示数组中的所有元素。我想我应该使用script标记,但是我不知道如何使用它。我什至都不懂JavaScript。无论如何,有没有人可以帮助您呢?这是我的python代码:

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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

有没有办法使用Angularjs在HTML元素中显示数组json对象

来自分类Dev

有没有办法使用css在html中隐藏没有id或class的元素?

来自分类Dev

有没有办法打印列表中某个类的所有元素?

来自分类Dev

有没有办法在 C++ 中并行循环遍历向量的所有元素?

来自分类Dev

有没有办法在PowerShell脚本中显示所有功能?

来自分类Dev

有没有办法在gitlab的索引页中显示所有子Wiki?

来自分类Dev

有没有办法使Int变量等于(==)数组中存储的所有数字?

来自分类Dev

有没有办法在ISim中显示变量?

来自分类Dev

有没有办法在 Javascript/HTML 中单击按钮更新现有数组?

来自分类Dev

有没有办法扩展swig / python中的所有类?

来自分类Dev

有没有办法在python脚本中获取变量的所有值?

来自分类Dev

有没有办法将数组的元素与Ruby中的哈希键匹配?

来自分类Dev

有没有办法使用 value(forKeyPath:) 访问 KVC 中的数组元素?

来自分类Dev

有没有办法从方法更新/返回数组中的元素?

来自分类Dev

有没有办法从方法更新/返回数组中的元素?

来自分类Dev

有没有办法只显示基于数组的json对象中的选定属性

来自分类Dev

有没有办法像这样在 MySQL 中显示数组?(“一”=>“1”)

来自分类Dev

有没有办法像我们在 python 中访问列表那样访问减索引数组元素?

来自分类Dev

有没有办法在Python中包含±(正负)?

来自分类Dev

有没有办法在python中禁止舍入?

来自分类Dev

有没有办法在HTML元素中获取javascript变量的值?

来自分类Dev

有没有办法(在 c# 中)遍历网站 HTML 上的文章元素?

来自分类Dev

有没有办法在html图像标签中显示位图数据?

来自分类Dev

有没有办法使用python列出json中未知数量的元素?

来自分类Dev

有没有办法在python中打印出JSON文件的某些元素?

来自分类Dev

有没有办法在单个查询中获取ActiveRecord Rails中数组条件的所有最后记录

来自分类Dev

有没有办法在Chrome开发人员工具中显示所有已定义的className?

来自分类Dev

有没有办法从列表中获取具有特定属性的所有项目

来自分类Dev

有没有办法合并Clickhouse列中的所有数组,然后过滤重复项?

Related 相关文章

  1. 1

    有没有办法使用Angularjs在HTML元素中显示数组json对象

  2. 2

    有没有办法使用css在html中隐藏没有id或class的元素?

  3. 3

    有没有办法打印列表中某个类的所有元素?

  4. 4

    有没有办法在 C++ 中并行循环遍历向量的所有元素?

  5. 5

    有没有办法在PowerShell脚本中显示所有功能?

  6. 6

    有没有办法在gitlab的索引页中显示所有子Wiki?

  7. 7

    有没有办法使Int变量等于(==)数组中存储的所有数字?

  8. 8

    有没有办法在ISim中显示变量?

  9. 9

    有没有办法在 Javascript/HTML 中单击按钮更新现有数组?

  10. 10

    有没有办法扩展swig / python中的所有类?

  11. 11

    有没有办法在python脚本中获取变量的所有值?

  12. 12

    有没有办法将数组的元素与Ruby中的哈希键匹配?

  13. 13

    有没有办法使用 value(forKeyPath:) 访问 KVC 中的数组元素?

  14. 14

    有没有办法从方法更新/返回数组中的元素?

  15. 15

    有没有办法从方法更新/返回数组中的元素?

  16. 16

    有没有办法只显示基于数组的json对象中的选定属性

  17. 17

    有没有办法像这样在 MySQL 中显示数组?(“一”=>“1”)

  18. 18

    有没有办法像我们在 python 中访问列表那样访问减索引数组元素?

  19. 19

    有没有办法在Python中包含±(正负)?

  20. 20

    有没有办法在python中禁止舍入?

  21. 21

    有没有办法在HTML元素中获取javascript变量的值?

  22. 22

    有没有办法(在 c# 中)遍历网站 HTML 上的文章元素?

  23. 23

    有没有办法在html图像标签中显示位图数据?

  24. 24

    有没有办法使用python列出json中未知数量的元素?

  25. 25

    有没有办法在python中打印出JSON文件的某些元素?

  26. 26

    有没有办法在单个查询中获取ActiveRecord Rails中数组条件的所有最后记录

  27. 27

    有没有办法在Chrome开发人员工具中显示所有已定义的className?

  28. 28

    有没有办法从列表中获取具有特定属性的所有项目

  29. 29

    有没有办法合并Clickhouse列中的所有数组,然后过滤重复项?

热门标签

归档