我开始学习 Flask 是为了一个大约一个月的小项目。我想做一个计算身体参数(如 BMI 或 LBM)的应用程序。问题是,当我请求表单中的数据时,它以元组的形式出现,因此 body_calculator 模块无法使用它,并在标题中抛出错误。我的问题是:为什么数据以元组的形式出现,在这种情况下,在 Flask 中请求数据的正确方法是什么?
烧瓶代码
from flask import Flask, url_for, render_template, make_response, request, redirect, session
import 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 = None
class 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)
慕村9548890
相关分类