Home > Net >  Get total of Select values in HTML to use with session in Python
Get total of Select values in HTML to use with session in Python

Time:07-22

I'm working on a practice assignment for my bootcamp, and I'm mostly finished with it, but can't quite figure out how to get a total value for Select values from HTML, then display them using session['count'] to put it in a print statement.

        <section >
          <div >
            <h1 >Thank you for your order!</h1>
            <p >Charging {{session['first_name']}} for {{session['count']}} fruits.</p>
          </div>
        </section>

@app.route('/checkout', methods=['POST'])         
def checkout():
    print(request.form)
    session['first_name'] = request.form['first_name']
    session['last_name'] = request.form['last_name']
    session['student_id'] = request.form['student_id']
    session['strawberry'] = request.form['strawberry']
    session['raspberry'] = request.form['raspberry']
    session['apple'] = request.form['apple']
    print(f"Charging {request.form['first_name']} for {{session['count'] fruits")
    return render_template("checkout.html")

I want the 'count' to be the sum of the selected values from 'strawberry', 'raspberry', & 'apple'. I tried something like session['count'] = request.form['strawberry', 'raspberry', 'apple'], but since it's a dictionary, it threw back an error on my localhost.

CodePudding user response:

if I understand correctly you need to retrieve values from your form under fields strawberry, raspberry and apple and them summarize values. To do so you get value from each field and you cannot do that with

request.form['strawberry', 'raspberry', 'apple']

You need to access value for each key, so for ex.:

session['count'] = request.form['strawberry']   request.form['raspberry']   request.form['apple']
  • Related