flask如何将后端数据传到前端?

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
{% extends "base.html" %}


{% block title %}详情{% endblock %}

{% block page_content %}
    <table class="table table-bordered">
    <tr>
        <th>编号</th>
        <th>标题</th>
        <th>内容</th>


    </tr>
        {% for i in u %}
            <tr>
                <td>{{ i[0] }}</td>
                <td>{{ i[1] }}</td>
                <td>{{ i[2] }}</td>




            </tr>
    {% endfor %}
    </table>

{% endblock %}
<form action=" search" method="post">
        <a>keyword:</a>:<input type="text" name="keyword">

        <button type="submit">搜索</button>
    </form>
def result(keyword):
    mydb = mysql.connector.connect(
    host="localhost",
    user="root",
    passwd="1234",
    database="oac"
    )


    mycursor = mydb.cursor()





    sql1 = ("SELECT m_id,m_title,m_content FROM message where concat(m_content,m_title) like '%%%s%%' " %keyword)

    mycursor.execute(sql1)

    u = mycursor.fetchall()         
    mydb.commit()

    return u

@app.route('/search',methods=["post","get"])
def search():
    keyword=request.form.get('keyword')
    res=result(keyword)
    print(res)
    return render_template('result.html',res=res)


一个基于flask的搜索框
后端输出的数据都是对的,但是HTML页面上没有显示

{% for res in entries %}
            <div class="l_item">
                <div class="con">
                    <a href="details?id={{ res.id }}"><div class="cover" style="background-image:url({{ res.thumb }});"></div></a>
                    <div class="info">
                        <h1 class="title"><a href="details?id={{ res.id }}">{{ res.name }}<span></span></a></h1>
                        <p><b>中文学名:</b>{{ res.name }}</p>
                        <p><b>拉丁学名:</b>{{ res.name_lad }}</p>
                        <p class="depict ellipsis"><b>描述:</b>{{ res.describe }}</p>
                    </div>
                </div>
            </div>
            {% endfor %}

entries相当于你的res

如果列数量不固定,不一定是三列,怎么写?

你在后端search函数render_template写的额外参数是res,那么在前端就不应该是

{% for i in u %}

因为u在前端未给出,而应该是

{% for i in res %}

另一种改法是将后端的res改为u。

https://blog.csdn.net/longting_/article/details/80658105