0

I can not get a history table to work. I'm confused about how to get each value that needs to be in each row of the table. Here is what I have right now, I just don't understand how to pull each value out of it. I think I can manage the html, I just need help with this. Thanks for any help!

def history():

#if request.method == "POST":

    #stock = db.execute("SELECT stock FROM history WHERE user_id = :user_id", user_id = session["user_id"])


    #purchases = db.execute("SELECT stock, boughtsold, price, shares, time, user_id FROM history WHERE user_id = :user_id", stock=stock, price=price, shares=shares, time=timestamp, user_id=session["user_id"] )
    #price = purchases["price"]
    purchases = db.execute("SELECT stock, boughtsold, price, shares, time, user_id FROM history WHERE user_id = :user_id", user_id=session["user_id"])



    for purchase in purchases:

        stock = purchases["stock"]
        boughtsold = purchase["boughtsold"]
        price = purchase['price']
        shares = purchase['shares']
        time = purchase['time']
        userid = purchase["user_id"]

#else:

    #stock = lookup(request.form.get("symbol"))
    return render_template("history.html", stock=stock, boughtsold=boughtsold, price=price, shares=shares, time=time, userid=userid)
1

You might consider sending the purchases list to the template and using a jinja for loop to iterate over the members to create the table rows.

So instead of sending each variable separately to the html like this:
return render_template("history.html", stock=stock, boughtsold=boughtsold, price=price, shares=shares, time=time, userid=userid)
it might be something like:
return render_template("history.html",purchases = purchases)

5
  • How would I go about sending the list to the HTML? – joelanbanks3 Aug 4 '19 at 2:56
  • Detail added to answer. – DinoCoderSaurus Aug 4 '19 at 12:22
  • OK so in the html I have a for loop, and my table is generated properly, and i filled it with test values. If i buy something again, a new row of test values is added, so that works. I sent the purchases over like you said, but what would be the proper way to access them on the hrml? I have something like For purchase in purchases... fill this data spot with purchase.userid but nothing comes up. Whats the right way to access the variable? – joelanbanks3 Aug 7 '19 at 21:00
  • If I had to guess, the correct name would be purchase.user_id because the dictionary keys will be the same as those returned from the sql. If that doesn't get things moving, please edit the html in to the question. – DinoCoderSaurus Aug 8 '19 at 10:42
  • It works great now, thanks! The problem was that I didn't think about how the variable names were case sensitive – joelanbanks3 Aug 8 '19 at 17:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .