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