Search code examples
pythonflaskflask-wtformswtforms

python flask wtf form return value from function


Trying to retrieve an int from a function which get the data from a form in wtform in Flask.

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!')

when I run the routes.py without the return a statement it shows the flash message just fine but when I try to return the (myproblem) integer it returns the following error

TypeError: The view function did not return a valid response. The return type must be a string, dict, tuple,

here is the function

@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)

I sort of understand what is says in the error message but I need to use the integers of each of the values (testing just with one of them to simplify) but I don't know how to do it.

I tried to search for similar issues in here but did find only one solution which did not work for me.

EDIT: I try to use this value(s) later in another function

@app.route('/slitter_results', methods=['GET', 'POST'])
@login_required

    myproblem = slitter()
    flash(myproblem)

Solution

  • I think you are looking for something like this:

    First, get the value from the form submitted and then pass the value in slitter_results function

    @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 function will look like this:

    @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!
    

    For more info have a look at Variable Rules and URL Building