python和sqlite返回了正确的数据,但HTML没有给出正确的数据

qnyhuwrf  于 2022-12-23  发布在  SQLite
关注(0)|答案(1)|浏览(134)

从数据库中获取一些东西,如果方法是get,它只会返回所有内容,但如果它的post,它会返回搜索到的内容,当它的get时,它工作正常,但当它的post时,它会给出正确的值,但html不会“破译”它?
超文本标记语言

{% for account in accounts %}
                <tr>
                    <th scope="col">{{ account["email"] }}</th>
                    <th scope="col">{{ account["username"] }}</th>
                    <th scope="col">{{ account["password"] }}</th>
                    <th scope="col">{{ account["website"] }}</th>
                    <th></th>
                </tr>
            {% endfor %}

AJAX

$(document).ready(function () {
        $("#search").on("input", function(e) {
            $(".table-body").empty();

            $.ajax ({
                method: "GET",
                url: "/manager",
                data: {search:$("#search").val()},
            })
        })
    })

Python

@app.route("/manager", methods=["GET", "POST"])
def manager():
    loggedUser = session["loggedUser"][0]["username"]
    if request.method == "GET":
        accounts = db.execute("SELECT * FROM manager WHERE loggedUser = ?", loggedUser)
    else:
        form = request.form.get("search")
        accounts = db.execute("SELECT * FROM manager WHERE loggedUser = ? AND website LIKE ?", loggedUser, form + "%")

    print(accounts)
    return render_template("manager.html", accounts=accounts)

它给出了正确的结果,但它不会在html中给出,除非它是get方法
POST时输出

[{'id': 1, 'loggedUser': 'Hyde', 'username': None, 'email': None, 'password': '123', 'website': 'HydPass'}]

结构

CREATE TABLE manager (id INTEGER, loggedUser TEXT NOT NULL, username, email, password TEXT NOT NULL, website TEXT NOT NULL, PRIMARY KEY (id));
7d7tgy0s

7d7tgy0s1#

结果我不得不用 AJAX 来更新
AJAX

$(document).ready(function () {
        $("#search").on("input", function(e) {
            $(".table-body").empty();

            $.ajax ({
                method: "POST",
                url: "/manager",
                data: {search:$("#search").val()},
                success: function(data) {
                    // Clear the table
                    $(".table-body").empty();

                    // Iterate over the returned data and add a row for each account
                    for (var i = 0; i < data.length; i++) {
                        var account = data[i];
                        var row = '<tr><th scope="col">' + account.email + '</th><th scope="col">' + account.username + '</th><th scope="col">' + account.password + '</th><th scope="col">' + account.website + '</th><th><input type="button" class="btn btn-primary p-2" value="Edit Account" name="edit"><input type="button" class="btn btn-warning p-1" value="Remove Account" name="remove"></th></tr>';
                        console.log(row)
                        $(".table-body").append(row);
                    }
                }
            })
        })
    })

相关问题