0

I have to show current value of my stock which is stored in my data base, I have to multiply Number of shares to current price here is my code

@app.route("/")
@login_required
def index():
#Remember id
ide = session["user_id"]
#take info about sahres user already bought
symb = db.execute ("select * from portfolio where id = :ii GROUP BY symbol ORDER BY symbol" , ii = ide)
#if user has not bought any sahres
if not symb:
    #Return user a message in html and his cash holdings
    row = db.execute ("select * from users where id = :ii" , ii = ide)
    return render_template ("noshares.html",cash = row[0]["cash"])
else:
    #list to save current price of stock
    curprice = []
    #Iterrate over each element in symb
    for share in symb:
        #Get symbol of each element
        symbol = share ["symbol"]

        #Lookup current price of that specific symbol/sahre
        quote = lookup (symbol)
        #save each current price
        curprice.append (quote["price"])
    #render values    
    return render_template ("index.html", symbol = symb,currentprices = curprice)

and here is html code

{% extends "layout.html" %}

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

{% block main %}

<table>
    <tr>
        <th>Stocks</th>
        <th>Shares</th>
        <th>Current Price</th>
        <th>Total</th>
    </tr>

    {% for stock in symbol %}
        <tr>
            <td>{{ stock["symbol"] }}</td>
            <td>{{ stock["shares"] }}</td>
            <td>{{ currentprices[stock] }}</td>
            <td>{{stock["shares"] * currentprices[stock]}} </td>
        </tr>
    {% endfor %}

{% endblock %}

This code shows error jinja2.exceptions.UndefinedError: list object has no element {'symbol': 'nflx', 'price': 281.78, 'shares': 2, 'id': 3}

In the error you can see the info from SQL data base and error is occuring on the last line which is

<td>{{stock["shares"] * currentprices[stock]}} </td>
1

The problem is that you are trying to access the list currentprices in quoted.html using a dictionary as the index. You are trying to return the value of the following list element, which obviously does not work:

currentprices[{'symbol': 'nflx', 'price': 281.78, 'shares': 2, 'id': 3}]

Think about making currentprices into a dictionary so that you can access the values using the symbol, use a counter to find the currentprices index you need or alternatively add the current price into the symbol dictionary.

5
  • I have used many mathods ti add current price into symbol but failed the last one is this symb['currentprice'] = quote ['price'] It should add another field in the dict symb called current price but error is "list indices must be integers, not str" kindly help me how can I do it.
    – Ahmed Raza
    Mar 12 '17 at 14:09
  • Isn't symb a list of dictionaries? So it would have to be symb[i]['current price'] = quote['price'] or similar right? Mar 12 '17 at 14:25
  • Sir I have tried every thing but can't determine how to execute this loop kindly help here on what to iterate for loop I have tried almost every thing... for share in symb: #Get symbol of each element symbol = share ["symbol"] #Lookup current price of that specific symbol/sahre quote = lookup (symbol) #save each current price for i in : symb['currentprice'] = quote ['price'] symb['total'] = quote ['price'] * share["shares"]
    – Ahmed Raza
    Mar 12 '17 at 17:53
  • Why not replace curprice.append (quote["price"]) with share["quoteprice"] = quote["price"], then you will be able to access the value in your html template with stock["quoteprice"]. Mar 12 '17 at 18:15
  • Thanks for your help and effort sir, respected.
    – Ahmed Raza
    Mar 13 '17 at 2:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .