首页 > 解决方案 > CS50 财务:/index 帮助显示正确的信息

问题描述

我看过一些关于 /index 的 CS50 Finance 帮助问题。我正在尝试获取显示用户拥有的股票信息(股票编号、价值、价格等)的路线。现在它显示正确的份额数量,但不显示名称、价值或价格(全为空白)。显示“cash”和“grandTotal”金额,但grandTotal 金额不正确。我想我只是对如何访问我的回报中的特定值感到困惑。

蟒蛇/sqlite:

def index():
    """Show portfolio of stocks"""
    # sql queries to select stock info
    user_id = session["user_id"]
    stocks = db.execute("SELECT stock, symbol, SUM(shares) AS totalShares FROM purchases WHERE userid == :userid GROUP BY symbol", userid=user_id)
    currentCash = db.execute("SELECT cash FROM users WHERE id == :userid", userid=user_id)

    # Global variables to be updated
    tableInfo = []
    grandTotal = currentCash[0]["cash"]

    #Grabbing info from each owned stock
    for stockInfo in stocks:
        symbol = stocks[0]["symbol"]
        shares = stocks[0]["totalShares"]
        name = stocks[0]["stock"]
        currentStock = lookup(symbol)
        price = currentStock["price"]
        value = price * shares
        grandTotal += value

        tableInfo.append(stockInfo)

    # Display a table with portfolio info for current user
    return render_template("index.html", tableInfo=tableInfo, grandTotal=usd(grandTotal), currentCash=usd(currentCash[0]["cash"]))

HTML:

{% extends "layout.html" %}

{% block title %}
    Your Portfolio
{% endblock %}

{% block main %}
    <table class="table">
      <thead>
        <tr>
          <th scope="col">Stock</th>
          <th scope="col">Number of shares</th>
          <th scope="col">Current price</th>
          <th scope="col">Total value</th>
        </tr>
      </thead>
      <tbody>

            {% for stock in tableInfo %}

            <tr>
                <td>{{ stock.name }}</td>
                <td>{{ stock.totalShares }}</td>
                <td>{{ stock.price }}</td>
                <td>{{ stock.value }}</td>
            </tr>

            {% endfor %}

      </tbody>
    </table>


    <table class="table">
        <thead>
            <tr>
                <th scope ="col">Cash remaining</th>
                <th scope ="col">Grand total</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>{{ currentCash }}</td>
                <td> {{ grandTotal }}</td>
            </tr>
        </tbody>
    </table>

{% endblock %}

标签: pythonhtmlsqliteflaskcs50

解决方案


推荐阅读