查阅html flas表格中的数据结束显示

2024-09-28 20:58:16 发布

您现在位置:Python中文网/ 问答频道 /正文

你好,我正在尝试这样做,输入一个值从html和采取一个值从数据库和结果显示在一个html表,但我没有得到他们,我会显示我的代码

Python

@app.route('/search', methods=['GET', 'POST'])
def search():
    if request.method == 'POST':
        try:
            sea = request.form['sea']

            with sql.connect("DRIVER={SQL Server}; SERVER=AUS_COMPUTO02\SQLEXPRESS; DATABASE=WEBSERVICE; Trusted_Connection = yes;") as con:
                cur = con.cursor()
                cur.execute("SELECT *FROM PROVEEDOR WHERE ID_PROVEEDOR = (?)", (sea).format(sea))
                sea=[dict(ID_PROVEEDOR=row[0], DECRIPCION=row[1]) for row in cur.fetchall()]
                cur.commit()
                msg = "Successfully added"
                print(sea)
        except Exception as e:
                              exit('error',e)

html格式

 <form class="form-inline my-2 my-lg-0" action="{{ url_for('search') }}" class="form-control">
      <input id="dt" name='sea' class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit" >Search</button> 
    </form>



<table class="table" action="{{ url_for('search') }}">
  <thead>
    <tr>
      <th scope="col">#</th>
      <th scope="col">ID</th>
      <th scope="col">Descipcion</th>
    </tr>
  </thead>
  <tbody>
  {% for s in slc %}
    <tr>
      <th scope="row">1</th>
      <td>{{ s.ID_PROVEEDOR }}</td>
       <td>{{ s.DECRIPCION }}</td>
    </tr>
    <tr>
    {% endfor %}
  </tbody>

Tags: formidforsearchmyhtmltrclass
1条回答
网友
1楼 · 发布于 2024-09-28 20:58:16

如果有人感兴趣,我会把这个解决方案留在这里

def search():

    search = request.args.get('sea')#This was the solution
    g.con = pyodbc.connect('DRIVER={SQL Server}; SERVER=AUS_COMPUTO02\SQLEXPRESS; DATABASE=WEBSERVICE; Trusted_Connection = yes;')
    cur = g.con.execute("SELECT *FROM PROVEEDOR WHERE ID_PROVEEDOR =(?)", (search))
    slc=[dict(ID_PROVEEDOR=row[0], DECRIPCION=row[1]) for row in cur.fetchall()]
    cur.commit()

    print('search')
    return render_template('t.html', slc=slc)

相关问题 更多 >