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页面上没有显示

2个回答

{% 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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐