咨询表格html烧瓶中的数据结尾显示

时间:2019-08-12 16:07:02

标签: python flask

您好,我尝试这样做,从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>

1 个答案:

答案 0 :(得分:0)

如果有人有兴趣,我将在这里留下解决方案。

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)