我是 Flask 的新手,我一直在尝试制作一个应用程序来处理输入,例如:身高、体重、性别、年龄和腰围,以便稍后输出一些身体参数
html 页面
{% extends "bootstrap/base.html" %}
{% block content %}
<html>
<head>
<title>Workout calculator</title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>
<form action="{{ url_for('routes.py') }}" method = "POST">
{{ form.csrf_token }}
<div class="wrapper">
<div class="title">
Workout calculator
</div>
<div class="form">
<div class="gender">
<label for="men">Men</label>
{{ form.sex }}
<label for="women">Women</label>
{{ form.sex }}
</div>
<div class="input_field">
<label for="height">Height</label>
{{ form.height }}
<select id="height">
<option value="cm">cm</option>
<option value="ft">ft</option>
</select>
</div>
<div class="input_field">
<label for="weight">Weight</label>
{{ form.weight }}
<select id="weight">
<option value="kg">kg</option>
<option value="lbs">lbs</option>
</select>
</div>
<div class="input_field">
<label for="age">Age</label>
{{ form.age }}
</div>
<div class="input_field">
<label for="waist">Waist</label>
{{ form.waist }}
<select id="waist">
<option value="cm">cm</option>
<option value="ft">ft</option>
</select>
</div>
</div>
<div class="input_field">
{{ form.submit(class="calculate") }}
</div>
</div>
</form>
<body>
<div class="results">
<div class="BMI">
<p>BMI (Body Mass Index): {{ BMI }} %</p>
</div>
<div class="Body fat">
<p>BFP (Body Fat Percentage): {{ BFP }} %</p>
</div>
<div class="Lean body mass">
<p>LBM (Lean body mass): {{ LBM }} %</p>
</div>
<div class="Waist / height ratio">
<p>BMR (Basal Metabolic Rate): {{ BMR }} %</p>
</div>
</div>
</body>
</body>
</html>
{% endblock content %}
routes.py
@app.route('/', methods = ['GET','POST'])
def index():
calc_form = InputData()
parameters = HealthBoard.body_calculator
height = float(request.form['height'])
weight = float(request.form['weight'])
age = float(request.form['age'])
sex = float(request.form['sex'])
waist = float(request.form['waist'])
body_params = parameters.Parameters(height, weight, age, sex, waist)
LBM = body_params.Lean_Body_Mass()
BMR = body_params.Basal_Metabolic_Rate()
BMI = body_params.Body_Mass_Index()
BFP = body_params.Body_Fat_Percentage()
context = {
'LBM' : LBM,
'BMR' : BMR,
'BMI' : BMI,
'BFP' : BFP,
'form': calc_form
}
if request.method == 'POST' and calc_form.validate_on_submit():
flash('It worked')
return render_template('index.html', **context)
forms.py
from flask_wtf import Form
from wtforms.fields import IntegerField, BooleanField, SubmitField
from wtforms.validators import DataRequired, InputRequired, ValidationError
class InputData(Form):
height = IntegerField('height', validators=[DataRequired()])
weight = IntegerField('weight', validators=[DataRequired(), InputRequired()])
age = IntegerField('age',validators=[DataRequired(), InputRequired()])
waist = IntegerField('waist', validators=[DataRequired(), InputRequired()])
sex = BooleanField('sex', validators=[])
submit = SubmitField('Submit')
我试过不使用 FlaskWTF 并且效果很好,但是当我使用它时出现此错误。
我尝试使用 return.form.get('') 并返回此错误
<块引用>TypeError: 不支持的操作数类型 *: 'NoneType' 和 'int'