0

I'm having some issues trying to render values on my index.html page. In my index function, I created a dictionary of values from my transactions table to grab each record of my share symbols, share names, share quantities based on the session user_id. I also ran a lookup based on symbols found in the dictionary I created. After that, I'm rendering the index template and passing these values to the template. In the index.html template, I created a for loop to iterate through each value, but I'm only able to display the table with header values, none of the values from my for loop are populating. I'm not sure what I'm missing here. I'm expecting to display a record for each item in my for loop but just getting blanks.

My index function:

@app.route("/", methods=["GET"])
@login_required
def index():
    """Show portfolio of stocks"""

    print(session["user_id"])
    if request.method == "GET":

        portfolio = db.execute("SELECT share_symbol, share_name, share_qty FROM transactions WHERE user_id=:user_id",
        user_id=session["user_id"])

        for record in portfolio:
            share_symbol = record['share_symbol']
            share_name = record['share_name']
            share_qty = record['share_qty']
            share = lookup(record['share_symbol'])
            price = share['price']
            symbol = share['symbol']

        return render_template("index.html",
        share_symbol=share_symbol, share_name=share_name, share_qty=share_qty, share=share, price=price, symbol=symbol)

HTML:

{% extends "layout.html" %}{% block title %} Purchased {% endblock %} {% block main %}
<table class="table" style="width:100%">

    <thead>
        <tr>
            <th scope="col">Symbol</th>
            <th scope="col">Name</th>
            <th scope="col">Shares</th>
            <th scope="col">Price</th>
            <th scope="col">Total</th>
        </tr>
    </thead>
    {% for record in portfolio %}
    <tbody>
        <tr>
            <td>{{ record.share_symbol }}</td>
            <td>{{ record.share_name }}</td>
        </tr>
    </tbody>
    {% endfor %}
    <tr>
        <th scope="col">CASH</th>
        <th scope="col"></th>
        <th scope="col"></th>
        <th scope="col"></th>
    </tr>
    </tbody>
</table>

{% endblock %}

index page

transactions table

Console

~/finance/ $ flask run
 * Serving Flask app "application.py" (lazy loading)
 * Environment: development
 * Debug mode: off
 * Running on https://abb12178-f914-4702-ad68-XXXXXXXXXXXXX-ide.cs50.xyz:8080/ (Press CTRL+C to quit)
 * Restarting with stat
INFO:werkzeug:192.168.112.187 - - [02/Aug/2020 19:18:20] "GET / HTTP/1.0" 302 -
INFO:werkzeug:192.168.112.187 - - [02/Aug/2020 19:18:20] "GET /login HTTP/1.0" 200 -
INFO:werkzeug:192.168.112.187 - - [02/Aug/2020 19:18:20] "GET /static/styles.css HTTP/1.0" 200 -
DEBUG:cs50:SELECT * FROM users WHERE username = 'angel'
INFO:werkzeug:192.168.112.187 - - [02/Aug/2020 19:18:28] "POST /login HTTP/1.0" 302 -
12
DEBUG:cs50:SELECT share_symbol, share_name, share_qty FROM transactions WHERE user_id=12
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): cloud-sse.iexapis.com:443
DEBUG:urllib3.connectionpool:https://cloud-sse.iexapis.com:443 "GET /stable/stock/amat/quote?token=pk_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX HTTP/1.1" 200 None
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): cloud-sse.iexapis.com:443
DEBUG:urllib3.connectionpool:https://cloud-sse.iexapis.com:443 "GET /stable/stock/ngvt/quote?token=pk_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX HTTP/1.1" 200 None
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): cloud-sse.iexapis.com:443
DEBUG:urllib3.connectionpool:https://cloud-sse.iexapis.com:443 "GET /stable/stock/ngvt/quote?token=pk_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX HTTP/1.1" 200 None
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): cloud-sse.iexapis.com:443
DEBUG:urllib3.connectionpool:https://cloud-sse.iexapis.com:443 "GET /stable/stock/ngvt/quote?token=pk_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX HTTP/1.1" 200 None
INFO:werkzeug:192.168.112.187 - - [02/Aug/2020 19:18:29] "GET / HTTP/1.0" 200 -
INFO:werkzeug:192.168.112.187 - - [02/Aug/2020 19:18:29] "GET /static/styles.css HTTP/1.0" 200 -
1

The problem is in the index function. The html is expecting a list named portfolio, but alas no such list is sent along with the render_template.

| improve this answer | |

You must log in to answer this question.

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