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?