mysql html flask,用于Mysql查询的FLASK HTML字段

Hello Stackoverflow community,

I am new to FLASK however while the learning curve has been very steep, there is one item that I have not been able to get my head around.

I am using a very simple HTML seach form, into which users type the name of a city, this input gets passed to a Mysql query and returns output into a Table.

Everything works except that I can't get the variable to pass into Mysql... if I fix the query it works.

I tried to work with FLASK WTForms, POST and GET requets, but I don't know where I am going wrong.

The variable data that I am passing is not confidencial, so I have no concern if it shows up in the URL.

Here just the simple FORM (I guess not correct)

Here the table output (working perfectly)

PO_NumberPlant NameGMIDMaterialINCOTERMVendorVendor Count

{% for row in tabledata %}

{{ row['PO_Number'] }}{{ row['PN'] }}{{ row['GD'] }}{{ row['MN'] }}{{ row['INCO'] }}{{ row['VNGS'] }}{{ row['CVNGS'] }}

{% endfor %}

Here the Python code

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)

Data Base Helper Mysql (the valye for PLN should change based on the input in the Form.

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()

Thank you in advance for any help.

解决方案

I think you need to set the action on the

element, rather than and you want to direct it to the same Flask endpoint (I assume?):

Update your helper class a little to accept a city variable from your view function (you could tighten this up a bit more):

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()

Then, update your view function to test if city is submitted and submit it to your helper class:

@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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值