有没有办法在 Python 中的 html 模板上 return 输出函数?

Is there a way to return function output on a html template in Python?

我正在使用以下代码:

def static_info(nm):
    cursor.execute("SELECT name,age,FROM MyDB where name like ?",(nm) + '%')
    for row in cursor:
        static={"NAME: ":row[0],"AGE: ":row[1]}
    return(static)

@app.route('/submit_form')
def submit_form():
    nm = request.form.get('name')
    info=static_info(nm)
    return render_template('static_display.html',info=info)

我需要在我的 static_display.html 页面上输出所需的函数(以字典的形式)

有什么建议吗?

奇怪的编码。

  1. 您在 static_info(nm) 中的循环只会 return 最后一项。您没有附加任何内容

  2. 您的 @app.route('/submit_form') 不接受 POST。您需要:

    @app.route('/submit_form', methods = ['GET', 'POST'])

  3. 在您的模板 static_display.html 中,您需要 {{ info }}

  4. 如果您更改代码以便可以遍历模板中的列表,您将需要如下内容:

    {% for item in info %} {% for key, value in item.items() %}
    {{键}}:{{值}}
    {% endfor %} {% endfor %}