我的python flask应用程序返回400 Bad请求

时间:2015-08-06 03:35:04

标签: python html sql flask

基本上在这个应用程序中我连接到我的sql数据库并尝试检索用户给出的输入结果,这些输入是AGE,SEX和ADMITTING DIAGNOSIS CODE。我能够为AGE和SEX提供用户输入并获得需要的结果但是当我为ADMITTING_DIAGNOSIS_CODE提供用户输入时,它返回400 BAD REQUEST。

我的Python代码:

import MySQLdb
from flask import Flask, render_template, request
from flask.ext.mysqldb import MySQL

app = Flask(__name__)

db = MySQLdb.connect("127.0.0.1","root","","health" )

@app.route("/", methods = ['GET','POST'])
def home():
    return render_template('home.html')




@app.route("/value", methods = ['GET','POST'])
def Authenticate():
    cursor = db.cursor()
    AGE = request.form['AGE']
    SEX = request.form['SEX']
    ADMITTING_DIAGNOSIS_CODE = request.form['ADMITTING_DIAGNOSIS_CODE']
    #DIAGNOSIS_CODE_1= request.args['DIAGNOSIS_CODE_1']

    sql = 'select avg(LENGTH_OF_STAY),avg(TOTAL_CHARGES),(select count(*) from health where AGE = 3 and SEX = 1 and ADMITTING_DIAGNOSIS_CODE = %s and DISCHARGE_STATUS = "A")/(count(*))*100 as alive,(select count(*) from health where AGE = 3 and SEX = 1 and ADMITTING_DIAGNOSIS_CODE = 5849 and DISCHARGE_STATUS = "B")/(count(*))*100 as dead from health where AGE = 3 and SEX = 1 and ADMITTING_DIAGNOSIS_CODE = 5849'

    entries = []
    cursor.execute(sql,(ADMITTING_DIAGNOSIS_CODE))

    # Fetch all the rows in a list of lists.
    results = cursor.fetchall()
    for row in results:
        entries.append(dict([('avg(LENGTH_OF_STAY)',row[0]),
                             ('avg(TOTAL_CHARGES)',row[1]),
                             ('dead',row[3]),
                             ('alive',row[2])

                             ]))

    return render_template('show_entries.html', entries=entries)

if __name__ == "__main__":
    app.debug = True
    app.run()

我的HTML代码:

**

> <form action="/value" method="post" enctype ="multipart/form-data">
> <div>Enter the Age <input type="text" name="AGE" style="border: 1px
> solid black"></div> <div>Enter the Sex <input type="text" name="SEX"
> style="border: 1px solid black"></div> <div>Enter the code <input
> type="text" name="ADMITTING_DIAGNOSIS_CODE" style="border: 1px solid
> black"></div> <div><input type="submit" value=" GO"></div> </form>

**

请帮帮我。

1 个答案:

答案 0 :(得分:0)

这一行

cursor.execute(sql,(ADMITTING_DIAGNOSIS_CODE))

应该是

cursor.execute(sql,(ADMITTING_DIAGNOSIS_CODE,))

不同之处在于(x)等于x而while(x,)等于包含第一个参数x的单个值元组。