Also, auth.user always contains the db.auth_user record of the current
logged in user (or None), and auth.user_id is the logged in user's id (see
http://web2py.com/book/default/chapter/08#Authentication). So, you don't
have to refer to session.auth.user or do a db query to pull the current user
record.
Anthony
On Wednesday, August 17, 2011 5:20:20 PM UTC-4, Massimo Di Pierro wrote:
> This
> {{if record.id == user.auth_user.default_card: print
> 'checked="checked''}}
>
> should be
>
> {{if record.id == auth.user.default_card:}} checked="checked" {{pass}}
>
> or
>
> {{if record.id == auth.user.default_card: response.write('
> checked="checked"')}}
>
> On Aug 17, 2:57 pm, Eric Scott <[email protected]> wrote:
> > I'm new to both Python and web2py and am having difficulty coding a
> > web2py radio form where each input field displays a name and an
> > image. One (and only one) of these images should be set as the user's
> > default image. I'm having trouble figuring out how to do this in
> > web2py. I was able to produce the dict and then loop through to list
> > them accordingly:
> >
> > CONTROLLER:
> > @auth.requires_login()
> > def show_cards():
> > records = db((db.card.owner == session.auth.user.id) |
> > (db.card.is_active == True)).select()
> > user = db(db.auth_user.id == session.auth.user.id).select()
> > return dict(records=records,user=user)
> >
> > VIEW (show_cards.html):
> >
> > {{for record in records:}} <input type='radio' name='mylist'
> > _value="{{=record.id}}"> <img src="{{=URL('download',
> > args=record.image)}}"/><br> {{pass}}
> >
> > MODEL:
> >
> > db.define_table('auth_user',
> > Field('id','id'),
> > Field('username', type='string',
> > label=T('Username')),
> > Field('password', type='password',
> > readable=False,
> > label=T('Password')),
> > Field('default_card', 'reference card')
> >
> > db.define_table('card',
> > Field('id','id'),
> > Field('image', 'upload',
> > uploadfield='image_file',label=T('Image')),
> > Field('image_file', 'blob'),
> > Field('nick',label=T('Nickname')),
> > Field('owner', db.auth_user),
> > Field('is_active','boolean',default=False,
> > label=T('Active')),
> >
> > The problem now is, I have no idea how to structure the view so that
> > "checked=checked" is printed if the card value is equal to the owner's
> > default value. I know it's going to involve a nested "if" clause, but
> > I'm not sure how to do this in a view. Something like: "{{if record.id
> > == user.auth_user.default_card: print 'checked="checked''}}" How
> > should I include this in the code? I've not yet seen how to format
> > nested logic in a view.
> >
> > I also think that I could do this with a widget, but the more I read
> > on radio buttons and widgets, the more lost I become. I know there
> > are probably much better ways to do this, but this is all I could hack
> > together.
> >
> > Any guidance would be greatly appreciated, even if it's just a pointer
> > in the right direction.
> >
> > Thanks in advance for your help.
> >
> > Eric