【问题标题】:can't make index function work in cs50 financecs50金融中无法使索引功能工作
【发布时间】:2019-12-24 02:45:10
【问题描述】:

我完全被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 %}

感谢您的帮助!

【问题讨论】:

标签: python html database jinja2 cs50


【解决方案1】:

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

在 html 中,您正在循环一个名为 transactions 的列表,此处为 {% for transaction in transactions %}

【讨论】:

  • 谢谢!我不敢相信我错过了:-/
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多