0

Here is my Flask Python application.

@app.route("/")
@login_required
def index():
    """Show portfolio of stocks"""

    holdings = db.execute("SELECT symbol,SUM(shares),price FROM purchases GROUP BY symbol HAVING id = ?",session["user_id"])

    return render_template("index.html",holdings=holdings)

Here is my HTML index file

{% extends "layout.html" %}

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

{% block main %}
<table class="table table-striped">
        <thead>
            <tr>
                <th>Symbol</th>
                <th>Name</th>
                <th>Shares</th>
                <th>Price</th>
                <th>TOTAL</th>
            </tr>
        </thead>
        <tbody>
            {% for trade in holdings %}
                <tr>
                    <td>{{trade.symbol}}</td>
                    <td>{{trade.SUM(shares)}}</td>
                    <td>{{trade}}</td>
                    <td>{{trade.price}}</td>
                    <td>$278.94</td>
                </tr>
            {% endfor %}
            <tr>
                <td colspan="4">CASH</td>
                <td>$9,589.93</td>
            </tr>
        </tbody>
        <tfoot>
            <tr>
                <td colspan="4"></td>
                <td>{{total}}</td>
            </tr>
        </tfoot>
    </table>
{% endblock %}

holding returns a list of the rows in a dictionary format like this: {'symbol': 'aapl', 'SUM(shares)': 1038, 'price': 137.27}. This is for every row.

Questions


Q1

When I try to access the second dictionary key SUM(shares) it does not work and an error pops up

(<td>{{trade.SUM(shares)}}</td> jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'SUM')

When I try it with a quotation around SUM(shares) this pops up

<td>{{trade.'SUM(shares)'}}</td> jinja2.exceptions.TemplateSyntaxError: expected name or number

I am using the CS50 IDE.

Why does this happen? How do I fix this?


Q2

To access the dictionary value, instead of doing dictionary["key"] like in Python, I did dictionary.key in Jinja. Is this normal Jinja or is this some specific modified Jinja for the course?

1 Answers1

0

Edit: because of the key value SUM(shares) containing characters that can be interpreted in multiple ways, you would need to switch to the your_dict['your_key'] accessing syntax instead of the "dot" accessing syntax:

            {% for trade in holdings %}
                <tr>
                    <td>{{ trade["symbol"] }}</td>
                    <td>{{ trade["SUM(shares)"] }}</td>
                    <td>{{ trade["price"] }}</td>
                </tr>
mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
  • Wait then what does trade.symbol do then? What is the dot for? – Rohan Muppa Jul 02 '21 at 21:43
  • Actually they do roughly the same thing in Jinja (reference: https://jinja.palletsprojects.com/en/3.0.x/templates/#variables), but there is the issue of your `SUM(shares)` key not being compatible with the "dot" syntax. So, you can just use the other syntax here. – mechanical_meat Jul 02 '21 at 21:56