无法使索引功能在CS50财务中工作

时间:2019-12-24 02:45:10

标签: python html database jinja2 cs50

我完全陷于使索引函数在CS50金融中起作用!此函数应在html页面中返回一个表格,其中包含在线交易的详细信息(详细信息保存在数据库中)。但这是行不通的:即使我的数据库中有事务,我的函数也找不到它,该表还是空的。 这是我的代码:

def index():
    """Show portfolio of stocks"""

    rows = db.execute("SELECT symbol, price, shares FROM transactions WHERE id = :user_id", user_id=session["user_id"])

    transactions_info = []
    total_worth = 0

    for transaction_info in rows:
        symbol = rows [0]["symbol"]
        shares = rows [0]["shares"]
        price = lookup(symbol)
        worth = shares * price ["price"]
        total_worth += worth
        transactions_info.append(transaction_info)


    return render_template("index.html", rows=rows, transactions_info=transactions_info)

这是我的HTML页面:

{% extends "layout.html" %}

{% block title %}
    Index
{% endblock %}

{% block main %}

   <table class="table table-striped" style="width:100%">
    <tr>
     <th>Symbol</th>
     <th>Company</th>
     <th>Shares</th>
     <th>Price</th>
     <th>TOTAL</th>
   </tr>
 {% for transaction in transactions %}
   <tr>
     <td>{{ transaction_info.symbol }}</td>
     <td>{{ transaction_info.name }}</td>
     <td>{{ transaction_info.shares }}</td>
     <td>{{ transaction_info.price }}</td>
     <td>{{ transaction_info.worth }}</td>
   </tr>
   {% endfor %}
 </table>
  {% endblock %}

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

index()中,您要在此处发送名为transactions_info的列表
 return render_template("index.html", rows=rows, transactions_info=transactions_info)

在html中,您正在transactions上遍历一个名为{% for transaction in transactions %}的列表。