Flask应用无法呈现数据库(psql)中的数据

时间:2019-02-22 20:46:51

标签: python html postgresql jinja2 psql

{% extends "layout.html" %}

{% block heading %}
Search Results
{% endblock %}

{% block body %}
fgrgrgtgtg
    <ul>
        {% for book in books %}
            <li>
                    {{ books.title }}
            </li>
        {% endfor %}
    </ul>
{% endblock %}
@app.route("/search", methods=["GET", "POST"])
def search():

        # Query database for book title
        books = db.execute("SELECT * FROM books WHERE title = :title",
                          {"title": request.form.get("title")}).fetchall()
        print (books)
        return render_template("search.html", books=books)

我正在为CS50W设计一个项目,被困住了。我正在尝试实现一个非常简单的搜索功能,该功能应按标题查找数据库中的所有书籍,然后在search.html上进行呈现。我只是想让它以最简单的方式工作,然后再开始添加更多的复杂性。

我在python代码中添加了“ print(books)”,以确保我已成功从数据库中提取数据,并且看来工作正常,因为它在终端中打印了预期的结果。但是我的html上的search.html上什么也没出现。

1 个答案:

答案 0 :(得分:0)

您有错字;循环中的变量为windows W.swapUp >> windows W.swapDown,而不是const express = require("express"); const bodyParser = require("body-parser"); const request = require("request"); const app = express(); app.use(express.static("public")); app.use(bodyParser.urlencoded({ extended: true })); app.get("/", function(req, res) { res.sendFile(__dirname + "/index.html"); }); app.post("/", function(req,res){ request("https://www.googleapis.com/calendar/v3/calendars/XXXXXXXXXXXX.calendar.google.com/events",function(response){ var calendarSummary = response.summary; var calendarDescription =response.description; console.log("Response is:"); console.log("Result is: "+calendarSummary+calendarDescription); }); }); app.listen(3000, function() { console.log("server is running on port 3000"); });

book