2016-12-16 154 views
2

你好社區#1,FLASK HTML領域MySQL查詢

我是新然而,燒瓶,同時學習曲線一直很陡,有一個項目,我一直無法左右我的頭。

我用一個非常簡單的HTML表單SEACH,在其中用戶鍵入一個城市的名字,這個輸入被傳遞到MySQL查詢並返回輸出到一個表。

一切正常,只是我不能得到變量傳遞到MySQL ...如果我解決它的工作原理查詢。

我試圖與FLASK WTForms,POST工作和GET requets,但我不知道我要去的地方錯了。 我傳遞的變量數據不是保密的,所以我不擔心它是否顯示在URL中。

這裏只是簡單的形式(我猜不正確)

<form> 
<div class="form-group"> 
    <div class="col-sm-3"> 
     <input type="text" placeholder="City Name" name="City_Name" action=/search class="form-control"> 
    </div> 

<div class="form-group"> 
    <div class="col-sm-2"> 
     <input type="submit" value="SEARCH" class="btn btn-primary btn-block"> 
    </div> 
    </div> 
</form> 

下面的表格輸出(正常使用)

<table class="table table-striped"> 


      <tr> 
       <th>PO_Number</th> 
       <th>Plant Name</th> 
       <th>GMID</th> 
       <th>Material</th> 
       <th>INCOTERM</th> 
       <th>Vendor</th> 
       <th>Vendor Count</th> 
      </tr> 


      {% for row in tabledata %} 
      <tr> 

      <td>{{ row['PO_Number'] }}</td> 
      <td>{{ row['PN'] }}</td> 
      <td>{{ row['GD'] }}</td> 
      <td>{{ row['MN'] }}</td> 
      <td>{{ row['INCO'] }}</td> 
      <td>{{ row['VNGS'] }}</td> 
      <td>{{ row['CVNGS'] }}</td> 
      </tr> 
      {% endfor %} 

    </table> 

這裏的Python代碼

from flask import Flask, render_template, request, url_for 
from dbhelper_single_search import DBHelper 


app = Flask(__name__) 
DB = DBHelper() 


@app.route('/table') 
def table(): 
    try: 
     tabledata = DB.table_inputs() 
    except Exception as e: 
     print(e) 
     tabledata = None 
    return render_template("table.html", tabledata=tabledata) 


if __name__ == '__main__': 
    app.run(port=5000, debug=True) 

數據庫Helper Mysql(PLN的valye應根據表單中的輸入更改。

import pymysql 
class DBHelper: 

def table_inputs(self): 
     connection = self.connect() 
     PLN="**City_Name**" 
     try: 
      query = "SELECT Plant_Geo, Plant_Code, Plant_Name, GMID, Material_Name, GROUP_CONCAT(DISTINCT Vendor_Name_GS ORDER BY Vendor_Name_GS) as VNGS, sum(2014_USD), sum(2015_USD), sum(2016_USD) FROM invoice_report WHERE plant_code like '%s' GROUP BY GMID ORDER BY sum(2015_USD) DESC" %(PLN); 
      with connection.cursor(pymysql.cursors.DictCursor) as cursor: 
       cursor.execute(query) 
      return cursor.fetchall() 
     finally: 
      connection.close() 

預先感謝您的任何幫助。

回答

0

我認爲你需要設置<form>元素上的動作,而不是<input>,你想直接到同一瓶端點(我認爲):

<form method="GET" action> 
    <div class="form-group"> 
     <div class="col-sm-3"> 
      <input type="text" placeholder="City Name" name="City_Name" class="form-control"> 
     </div> 
    </div> 
    <div class="form-group"> 
     <div class="col-sm-2"> 
      <input type="submit" value="SEARCH" class="btn btn-primary btn-block"> 
     </div> 
    </div> 
</form> 

更新助手類一點點接受從您的視圖功能的城市變量(可以多一點收緊這件事):

import pymysql 


class DBHelper: 

    def table_inputs(self, city): 
     connection = self.connect() 
     PLN = "**%s**" % city 
     try: 
      query = "SELECT Plant_Geo, Plant_Code, Plant_Name, GMID, Material_Name, GROUP_CONCAT(DISTINCT Vendor_Name_GS ORDER BY Vendor_Name_GS) as VNGS, sum(2014_USD), sum(2015_USD), sum(2016_USD) FROM invoice_report WHERE plant_code like '%s' GROUP BY GMID ORDER BY sum(2015_USD) DESC"; 
      with connection.cursor(pymysql.cursors.DictCursor) as cursor: 
       # actually better to pass parameters like this: 
       cursor.execute(query, (PLN,)) 
      return cursor.fetchall() 

     except Exception as err: 
      # this may also help find errors generated above... 
      print(err) 

     finally: 
      connection.close() 

然後,更新您的視圖功能測試,如果城市被提交,並提交您的輔助類:

@app.route('/table') 
def table(): 
    // the second argument is the default if "City_Name" is not submitted 
    city = request.args.get('City_Name', 'New York') 
    try: 
     tabledata = DB.table_inputs(city) 
    except Exception as e: 
     print(e) 
     tabledata = None 
    return render_template("table.html", tabledata=tabledata) 
+0

PJ桑託羅,太感謝你了!這工作100%。很大的幫助,真的很感激。 –

+0

太棒了,很高興我能幫到你!如果你能接受那個很好的答案。 – abigperson

+0

PJ Santoro已經有一段時間了,我在去年使用了這段代碼。然而,最近我升級到macOS Sierra和MariaDB 10.2.9,代碼(完全相同)現在拋出一個「TypeError:'NoneType'對象不可迭代」。我無法弄清楚爲什麼,但如果您有任何想法,我將不勝感激。 –