Another quick question. I've now stored prices.ALL into a variable
'prices' and passed to the view. I want to show each share as a line
with a subline for each price associated to that share. Here is the
code:

{{extend 'layout.html'}}
<h1>Share portfolio</h1>
<ul>
{{for share in shares:}}
{{=LI(share.asx_code)}}
<ul>
{{for price in prices:}}
{{=LI(price.price_close)}}
{{pass}}
</ul>
{{pass}}
</ul>

(price_close is the value I want to show). This is how it comes out
and the closest I can get it:

    * GDA
          o 3
          o 67
          o 45
          o 31
    * VZZ
          o 3
          o 67
          o 45
          o 31

How do I alter the view to only make a new 'price_close' line if the
price belongs to that share? They are linked here:

Field('share_id',db.shares)

Thanks!

On May 3, 2:29 pm, ztd <zac.thompsondav...@gmail.com> wrote:
> With the above two solutions I cleaned up the code and it now works.
>
> I had changed this:
> [IS_NOT_EMPTY(),IS_NOT_IN_DB(db,db.shares.asx_code)]
> to this
> [IS_NOT_EMPTY(),IS_NOT_IN_DB(db,'shares.asx_code')]
>
> and this:
> {{for asx_code in shares:}}
> {{=LI(shares.asx_code)}}
> {{pass}}
>
> to this:
> {{for share in shares:}}
> {{=LI(share.asx_code)}}
> {{pass}}
>
> After this I'm getting a list of each share, finally! As soon as read
> your suggestion Nathan I twigged that 'share' is a variable created in
> the loop just for the loop - the naming had me thinking I was
> referencing a table or the earlier dict variable. So for reference, in
> the code above, 'share' is just a counter and '.asx_code' is the
> attribute, so the written flow is
>
> For every row ('share' variable) found in the dictionary
> 'shares' (from the controller), show the asx_code variable for the
> current row.
>
> On May 2, 9:27 pm, Nathan Freeze <nat...@freezable.com> wrote:
>
> > You're not using the iteration variable.
>
> > This line:
> > {{=LI(shares.asx_code)}}
>
> > should be:
> > {{=LI(asx_code.asx_code)}}
>
> > But a more logical iteration variable would be 'share':
>
> > {{extend 'layout.html'}}
> > <h1>Share portfolio</h1>
> > <ul>
> > {{for share in shares:}}
> > {{=LI(share.asx_code)}}
> > {{pass}}
> > </ul>
>
> > On Sun, May 2, 2010 at 11:02 AM, ztd <zac.thompsondav...@gmail.com> wrote:
> > > Hi,
>
> > > here is an error I receive in a simple stock tracking application I'm
> > > making to help me learn Web2Py:
>
> > > Traceback (most recent call last):
> > >  File "gluon/restricted.py", line 178, in restricted
> > >  File "C:\Users\Zac\Desktop\web2py\applications\shares/views\default/
> > > index.html", line 86, in <module>
> > > AttributeError: 'Rows' object has no attribute 'asx_code'
>
> > > I think the problem is to do with passing variables between the
> > > controller and view. I've based this app on the 'image blog' example
> > > in the book. Here is my code so far:
>
> > > ---db.py---
>
> > > db.define_table('shares',
> > >    Field('asx_code'),
> > >    Field('date_added','datetime',default=request.now),
> > >    Field('quantity'),
> > >    Field('buy_price'))
>
> > > db.define_table('prices',
> > >    Field('share_id',db.shares),
> > >    Field('date','datetime',default=request.now),
> > >    Field('price_low',default='0'),
> > >    Field('price_high',default='0'),
> > >    Field('price_close',default='0'))
>
> > > db.shares.asx_code.requires =
> > > [IS_NOT_EMPTY(),IS_NOT_IN_DB(db,db.shares.asx_code)]
> > > db.shares.quantity.requires = IS_NOT_EMPTY()
> > > db.shares.buy_price.requires = IS_NOT_EMPTY()
>
> > > db.prices.share_id.requires = IS_IN_DB(db,db.shares.id,'%(asx_code)s')
>
> > > db.prices.share_id.writable = db.prices.share_id.readable = False
>
> > > ---index in default.py---
>
> > > def index():
> > >    shares=db().select(db.shares.ALL, orderby=db.shares.asx_code)
> > >    return dict(shares=shares)
>
> > > ---default/index.html---
>
> > > {{extend 'layout.html'}}
> > > <h1>Share portfolio</h1>
> > > <ul>
> > > {{for asx_code in shares:}}
> > > {{=LI(shares.asx_code)}}
> > > {{pass}}
> > > </ul>
>
> > > What am I doing wrong? i'm a bit confused with variables in the
> > > controller, I have a table called shares, a variable declared in the
> > > controller called shares and the value in the dictionary called shares
> > > so it's very hard to know what I'm calling / using in the view!!! (the
> > > LI code could be wrong, I couldn't get it to show anything to debug
> > > that far). shares table contains two entries.
>
> > > Thanks!

Reply via email to