开发者

web2py How can I use images in radiobuttons generated from a SQLFORM

开发者 https://www.devze.com 2023-03-01 12:02 出处:网络
I would like to generate a SQLFORM with radiobuttons using the radiobutton widget as below (code trimmed):

I would like to generate a SQLFORM with radiobuttons using the radiobutton widget as below (code trimmed):

db.define_table('tabA',
    Field('name', 'string', length=16, required=True, unique=True, ),
    Field('shape', 'string', length=8, default='star', widget=SQLFORM.widgets.radio.widget, requires=IS_IN_SET({'st开发者_运维百科ar' : '<img src="/myapp/static/images/shapes/star1.png" />'})))

My problem is that the image code above gets sanitized and the image tag doesn't show up. Can I turn off the sanitization for SQLFORM somehow? Or is there a different elegant solution to this? My code above shows only 1 radiobutton item for simplicity, but the items are generated dynamically.


You can replace '<img src="/myapp/static/images/shapes/star1.png" />' with one of the following two options:

  • XML('<img src="/myapp/static/images/shapes/star1.png" />')

  • IMG(_src='/myapp/static/images/shapes/star1.png')

Also, it is generally preferable to use the URL() function to generate outgoing URLs. With that replacement, the above two options would change to:

  • XML('<img src="' + URL('static', 'images/shapes/star1.png') + '" />')

  • IMG(_src=URL('static', 'images/shapes/star1.png'))

For more information, see the online book topics on XML() and the IMG helper.

0

精彩评论

暂无评论...
验证码 换一张
取 消