I'm working on the buy() method. I've created an input form allowing the user to enter the share (what we call "stocks" in the UK) symbol and the number of shares the user wants to buy.

I then want to go a little beyond the spec and add an intermediate step that:

  1. Looks up the shares (what we call "stocks" in the UK)
  2. Displays an HTML table showing the symbol, company name, share price, the number of shares the user wants to buy, the total cost (share price * number) and the user's current cash balance (from finance.db)
  3. Have a button saying "Please Confirm you Want to Buy" [or something like that]

I've created the HTML table and can pass in the symbol, company name and current share price - via the lookup() method. But I don't know how to pass in the number of shares and total cost variables into the same table - though I know they are calculated using the #'d out print() commands you'll see below. [NB, I haven't started on getting the current cash balance from finance.db yet].

My screens look like:

Buy Form and:

Table from "buying.html

My python and html code are below. Many thanks in advance for your help.

buy() - in application.py

@app.route("/buy", methods=["GET", "POST"])
def buy():
    """Buy shares of stock."""

# if user reached route via POST (as by submitting a form via POST)
if request.method == "POST":

    # ensure share symbol was submitted
    if not request.form.get("symbol"):
        return apology("You must submit a share symbol")

    # ensure number of shares to buy was submitted    
    elif not request.form.get("num_shares_buy"):
        return apology ("You must submit a number of shares to buy")

    # retrieve the number of shares to buy
    num_shares_buy = int(request.form.get("num_shares_buy"))

    # ensure number of shares to buy is a positive integer    
    if num_shares_buy < 1:   
        return apology ("You must submit a positive whole number of shares to buy")

    #print("XXXXXXX YOU ARE HERE XXXXXXXX number of shares to buy is {}".format(num_shares_buy))

    # lookup symbol using lookup() method
    buy = lookup(request.form.get("symbol"))
    #print("XXXXXXXX YOU ARE HERE XXXXXXXXXX price is {}".format(buy["price"]))

    # calculate cost of shares to buy (number x price)
    cost = num_shares_buy * buy["price"]
    #print("XXXXXXX YOU ARE HERE XXXXXXX Total Cost is {}".format(cost))

    # ensure symbol entered is valid (i.e. returns data when lookup() queries yahoo finance)
    if buy == None:
        return apology("You must submit a valid share symbol")

    # print the details of the desired share purchase
        return render_template("buying.html", placeholder = buy)

# else if user reached route via GET (as by clicking a link or via redirect)
    return render_template("buy.html")


{% extends "layout.html" %}

{% block title %}
    Share Purchase In Progress
{% endblock %}

{% block main %}
            <th>Price ($US)</th>
            <th>Number of Shares to Buy</th>
            <th>Cost ($US)</th>
            <th>Cash Available ($US)</th>

            <td>CASH - TO COME</td>

{% endblock %}

1 Answer 1


From the flask doc (Rendering Templates):

To render a template you can use the render_template() method. All you have to do is provide the name of the template and the variables you want to pass to the template engine as keyword arguments.

Notice the plurals. You can send multiple variables to render_template like render_template("buying.html", placeholder = buy, var1 = something, var2 = somethingelse), then access var1 and var2 in the html.

Another option is to add the "new" fields as key/value pairs to the buy dictionary, like buy["var1"] = something.

  • Thank you so much! I even read and tried that, but I must have got the syntax wrong somewhere. Really helpful just having someone point me back in the right direction. May 24, 2017 at 15:09
  • Sorry, realise my ego was getting in the way there - I could and should have just said 'Thank You'! Especially for providing two different solutions that I could try - and thereby learn more. May 24, 2017 at 15:24
  • You're welcome in every case! : ) May 24, 2017 at 16:17

You must log in to answer this question.

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