python flask wtf 形式 return 来自函数的值
python flask wtf form return value from function
尝试从函数中检索一个 int,该函数从 Flask 中的 wtform 中的表单获取数据。
form.py
class CoilsSlittingMetric(FlaskForm):
masterCoil = IntegerField('Master Coil Size [mm]', validators=[DataRequired()])
slitOne = IntegerField('First Coil Size [mm]', validators=[DataRequired()])
slitTwo = IntegerField('Second Coil Size [mm]', validators=[DataRequired()])
slitThree = IntegerField('Third Coil Size [mm]', validators=[DataRequired()])
slitFour = IntegerField('Fourth Coil Size [mm]', validators=[DataRequired()])
remember = BooleanField('Remember my current entries?')
submit = SubmitField('Calculate!')
当我 运行 routes.py 没有 return 语句时它显示闪现消息就好了
但是当我尝试 return (myproblem) 整数时,它 return 出现以下错误
TypeError: The view function did not return a valid response. The return type must be a string, dict, tuple,
这里是函数
@app.route('/slitter', methods=['GET', 'POST'])
@login_required
def slitter():
form = CoilsSlittingMetric()
if form.validate_on_submit():
myproblem = form.masterCoil.data
flash(myproblem)
flash(f'Calculating {form.masterCoil.data} '
f'and resulting data {form.slitOne.data}, {form.slitTwo.data}, '
f'{form.slitThree.data}, {form.slitFour.data}')
return myproblem
return redirect(url_for('slitter_results'))
return render_template('slitter.html', title="Slitter Calculator - Metric Sizes", form=form)
我有点理解错误消息中的内容,但我需要使用每个值的整数(仅使用其中一个值进行测试以简化),但我不知道该怎么做。
我试图在这里搜索类似的问题,但只找到了一个对我不起作用的解决方案。
编辑:
我稍后尝试在另一个函数中使用这个值
@app.route('/slitter_results', methods=['GET', 'POST'])
@login_required
myproblem = slitter()
flash(myproblem)
我想你正在寻找这样的东西:
首先,从提交的表单中获取值,然后将值传递给slitter_results
函数
@app.route('/slitter', methods=['GET', 'POST'])
@login_required
def slitter():
form = CoilsSlittingMetric()
if form.validate_on_submit():
myproblem = form.masterCoil.data
flash(f'Calculating {form.masterCoil.data} '
f'and resulting data {form.slitOne.data}, {form.slitTwo.data}, '
f'{form.slitThree.data}, {form.slitFour.data}')
return redirect(url_for('slitter_results', param_int=myproblem))
return render_template('slitter.html', title="Slitter Calculator - Metric Sizes", form=form)
slitter_results
函数将如下所示:
@app.route('/slitter_results/<int: param_int>', methods=['GET', 'POST'])
@login_required
def slitter_results(param_int)
myproblem = param_int
flash(myproblem)
return "Yay" #just so you don't get an error, you can render or redirect to anywhere from here!
有关详细信息,请查看 Variable Rules and URL Building
尝试从函数中检索一个 int,该函数从 Flask 中的 wtform 中的表单获取数据。
form.py
class CoilsSlittingMetric(FlaskForm):
masterCoil = IntegerField('Master Coil Size [mm]', validators=[DataRequired()])
slitOne = IntegerField('First Coil Size [mm]', validators=[DataRequired()])
slitTwo = IntegerField('Second Coil Size [mm]', validators=[DataRequired()])
slitThree = IntegerField('Third Coil Size [mm]', validators=[DataRequired()])
slitFour = IntegerField('Fourth Coil Size [mm]', validators=[DataRequired()])
remember = BooleanField('Remember my current entries?')
submit = SubmitField('Calculate!')
当我 运行 routes.py 没有 return 语句时它显示闪现消息就好了 但是当我尝试 return (myproblem) 整数时,它 return 出现以下错误
TypeError: The view function did not return a valid response. The return type must be a string, dict, tuple,
这里是函数
@app.route('/slitter', methods=['GET', 'POST'])
@login_required
def slitter():
form = CoilsSlittingMetric()
if form.validate_on_submit():
myproblem = form.masterCoil.data
flash(myproblem)
flash(f'Calculating {form.masterCoil.data} '
f'and resulting data {form.slitOne.data}, {form.slitTwo.data}, '
f'{form.slitThree.data}, {form.slitFour.data}')
return myproblem
return redirect(url_for('slitter_results'))
return render_template('slitter.html', title="Slitter Calculator - Metric Sizes", form=form)
我有点理解错误消息中的内容,但我需要使用每个值的整数(仅使用其中一个值进行测试以简化),但我不知道该怎么做。
我试图在这里搜索类似的问题,但只找到了一个对我不起作用的解决方案。
编辑: 我稍后尝试在另一个函数中使用这个值
@app.route('/slitter_results', methods=['GET', 'POST'])
@login_required
myproblem = slitter()
flash(myproblem)
我想你正在寻找这样的东西:
首先,从提交的表单中获取值,然后将值传递给slitter_results
函数
@app.route('/slitter', methods=['GET', 'POST'])
@login_required
def slitter():
form = CoilsSlittingMetric()
if form.validate_on_submit():
myproblem = form.masterCoil.data
flash(f'Calculating {form.masterCoil.data} '
f'and resulting data {form.slitOne.data}, {form.slitTwo.data}, '
f'{form.slitThree.data}, {form.slitFour.data}')
return redirect(url_for('slitter_results', param_int=myproblem))
return render_template('slitter.html', title="Slitter Calculator - Metric Sizes", form=form)
slitter_results
函数将如下所示:
@app.route('/slitter_results/<int: param_int>', methods=['GET', 'POST'])
@login_required
def slitter_results(param_int)
myproblem = param_int
flash(myproblem)
return "Yay" #just so you don't get an error, you can render or redirect to anywhere from here!
有关详细信息,请查看 Variable Rules and URL Building