Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
How can I keep user input on form on a failed post request in flask

I am building a website that will accept user input in 7 fields, one of them is a list of stocks (can be up to 100). One of the fields the user should input is a target of annual rate of return.

The issue I am having is that if the target of annual rate of return failed (it is larger than the max return) I am flashing the user with a message, and redirecting to the form, but I lose all pre entered input feilds the user submitted.

What I would like to do is to keep all the input the user submitted and flash the message under the "target of annual rate of return" field in the form.

What I have so far is on HTML:

<h3> Please enter target annual rate of return</h3>
    <div class="form-group">
        <input autocomplete="off" autofocus class="form-control" name="return" placeholder="rate of return" type="number">%
    </div>
    <button onClick="this.form.submit(); this.disabled=true; this.value='Sending…'; " class="btn btn-primary" type="submit">Build</button>

on the app.py I have:

ef = EfficientFrontier(mu, S, weight_bounds=(None, None))
    ef.add_objective(objective_functions.L2_reg)
    try:
        ef.efficient_return(target_return=(float(request.form.get("return"))/100), market_neutral=True)
    except pypfopt.exceptions.OptimizationError:
        flash('Target annual rate of return is too high for this portfolio, please consider using a lower rate')
        return redirect("/build")

so hopefully I will have the flash appearing and the form input intact when except "pypfopt.exceptions.OptimizationError:" is triggered.

Thanks!

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!