Skip to content
Advertisement

So my Jinga code is not outputing an option

This is a cs50 pset if you didn’t know.

{% extends "layout.html" %}

{% block title %}
    Log In
{% endblock %}

{% block main %}
    <form method="post">
        <div class="form-group">
            <select class="custom-select" style="width: auto;" name="select">
                <option value="" disabled selected>Symbol</option>
                {% for stock in symbols %}
                    <option value={{ stock }}>{{ stock }}</option>
                {% endfor %}
            </select>
        </div>
        <div class="form-group">
            <input class="form-control" name="shares" placeholder="Shares" type="text">
        </div>
        <button class="btn btn-primary" type="submit">Sell</button>
    </form>
{% endblock %}

Here is the app.py

@app.route("/sell", methods=["GET", "POST"])
@login_required
def sell():

    user = db.execute("SELECT username FROM users WHERE users.id=?", session['user_id'])[0]['username']

    if request.method == "GET":
        symbols = []
        unfiltered_stocks = db.execute("SELECT symbol FROM purchases WHERE user=?", user)

        for stock in unfiltered_stocks:
            symbols.append(stock['symbol'])

        return render_template("sell.html")

You can consider that stocks is an array and its not empty

The Output is just the first placeholder option and nothing more

Advertisement

Answer

When you call render_template(), you can pass Python variables to it which makes them available in your Jinja template.

Change return render_template("sell.html") to return render_template("sell.html", symbols=symbols)

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement