So, as the title says I can't figure out what I'm doing wrong to give the user a drop-down menu which contains the symbols of his/her stocks.

Thank you for your time in advance!

My intention is: -query database for his/her stocks -store it in a dict object as key->value pairs -pass it to sell.html via flask

Here is my sell() function:

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

    # Get the stock symbols that user have in order to use it in "sell.html"
    options = db.execute("SELECT stock_name FROM purchases WHERE buyer_id = :id")

    # Create a dict with key->value pairs
    for option in options:
        name = ""
        option["name"] = option["stock_name"]

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

        # TODO
        flash("Successfully sold!")
        return redirect("/")
    # Else, user reached route via GET (as by clicking a link or via redirect)
        return render_template("sell.html", options = options)

And here is my sell.html:

> {% extends "layout.html" %}

{% block title %}
{% endblock %}

{% block main %}
    <form action="/sell" method="post">
        <div class="form-group">
            <div class="input-group">
                <span class="input-group-adon">Select the stock symbol to sell</span>
                <select name="symbol" class="selectpicker form-control">
                    {% for option in options %}
                    <option value={{option["name"]}}>{{ option["name"] }}</option>
                    {% endfor %}
        <button class="btn btn-danger">Sell Stock</button>
{% endblock %}

What is this acutally building?

for option in options:
        name = ""
        option["name"] = option["stock_name"]

This will create a list that looks like this:

[{'stock_name': 'A', '': 'A'}, {'stock_name': 'B', '': 'B'}]

So when it gets to the html, there is no name index/key in options

Isn't options as returned from the select already what you want/need?

  • Yes, first I tried exactly as you suggested. I knew that I was getting the information I needed via options, but the problem was that line of my sell.html: <option value="{{option["stock_name"]}}">{{ option["stock_name"] }}</option> I thought that as I was seeing stock_name in red color, it was being interpreted as an HTML element so I tried to escape it via ``. Now I wrote it without escaping and it turns out that I'm a total noob :D Thank you for your time and for your effort. You saved me from going crazy. – Dement0 Jul 11 '18 at 17:34

You must log in to answer this question.

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