我開始學(xué)習(xí) Flask 是為了一個(gè)大約一個(gè)月的小項(xiàng)目。我想做一個(gè)計(jì)算身體參數(shù)(如 BMI 或 LBM)的應(yīng)用程序。問題是,當(dāng)我請求表單中的數(shù)據(jù)時(shí),它以元組的形式出現(xiàn),因此 body_calculator 模塊無法使用它,并在標(biāo)題中拋出錯(cuò)誤。我的問題是:為什么數(shù)據(jù)以元組的形式出現(xiàn),在這種情況下,在 Flask 中請求數(shù)據(jù)的正確方法是什么?燒瓶代碼from flask import Flask, url_for, render_template, make_response, request, redirect, sessionimport body_calculator app = Flask(__name__, static_folder='static',template_folder='templates') @app.route('/', methods = ['GET','POST'])def index(): if request.method == 'POST': height = int(request.form['height']), weight = int(request.form['weight']), age = int(request.form['age']), sex = bool(request.form['sex']), waist = int(request.form['waist']) body = body_calculator.Parameters(height, weight, age, sex, waist) LBM = body.Lean_Body_Mass() BMR = body.Basal_Metabolic_Rate() BFP = body.Body_Fat_Percentage() BMI = body.Body_Mass_Index() context = { 'height' : height, 'weight' : weight, 'age' : age, 'sex': sex, 'waist' : waist, 'LBM' : LBM, 'BMR' : BMR, 'BMI' : BMI, 'BFP' : BFP } return render_template('index.html', **context) else: return render_template('index.html')if __name__ == "__main__": app.run(debug=True)body_calculator模塊 BMI = Noneclass Parameters: def __init__(self, height, weight, age, sex, waist): self.height = height self.weight = weight self.age = age self.sex = sex self.waist = waist # Body Lean Mass function def Lean_Body_Mass(self): if self.sex == 0: BLM = (0.3281 * self.weight) + (0.33929 * self.height) - 29.5336 return round(BLM,2)
1 回答

慕村9548890
TA貢獻(xiàn)1884條經(jīng)驗(yàn) 獲得超4個(gè)贊
這幾行看起來有問題:
height?=?int(request.form['height']),?? weight?=?int(request.form['weight']),? age?=?int(request.form['age']),? sex?=?bool(request.form['sex']),
請注意,您編寫了一個(gè)尾隨逗號,它將值放入length-1 元組中:因此,例如第一行height
從表單中獲取,將其轉(zhuǎn)換為 an?int
,然后將其放入 length-1 元組中。嘗試刪除那些結(jié)尾的逗號。
添加回答
舉報(bào)
0/150
提交
取消