Web2py表单字段选项

编程入门 行业动态 更新时间:2024-10-25 20:30:25
本文介绍了Web2py表单字段选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用的是web2py表单,我希望有一些只对用户可见的字段(固定不可编辑)。我尝试过编辑,可写,只读的各种组合,但没用。我也查看了web2py书籍,但这似乎还不够。如果有人能告诉我如何做到这一点,那将是非常棒的。

解决方案

只有在登录后才可见?

如果是这种情况,请有条件地构建您的表单:

form_fields = [ Field('pubfield'), Field('pubfield2')] 如果auth.user:#This如果最终用户登录并且您使用的是内置身份验证 form_fields.append(Field('private_field')) return dict(form = FORM( form_fields))

除非你不是在谈论登录用户,只是想让这些字段可见,但不可编辑。然后,像您尝试的那样使用writable = False,但我认为您必须使用crud.create / crud.update或SQLFORM / SQLFORM.factory(后者不需要数据模型)。​​

SQLFORM.factory(Field('my_readable_field',writable = False))

如果表单基于数据库,则可以使用CRUD(如果不使用验证,则需要修改CRUD的设置,以便可以访问CRUD表单)

crud.create(db.some_table)

$或b $ b $ $ $ $ $ p $ $ $ $ $ c>

I am using web2py forms and i want to have some fields only visible to user (as fixed which cannot be edited). I tried making various combinations of editable, writeable, readonly but was of no use. I looked into web2py book too but that also seems insufficient. It would be great if someone can tell me how to do this.

解决方案

You mean some fields visible to all visitors and some fields visible only if logged in?

If that's the case, then build your form conditionally:

form_fields = [ Field('pubfield'), Field('pubfield2') ] if auth.user: # This is true if the end-user is logged in and you're using the built-in auth form_fields.append(Field('private_field')) return dict(form=FORM(form_fields))

Unless you're not talking about logged in users, and just want to make the fields be visible, but not editable. Then, use writable=False like you tried, but I think you have to either use crud.create/crud.update or SQLFORM / SQLFORM.factory (the latter does not require a data model)

SQLFORM.factory(Field('my_readable_field', writable=False))

If you the form is based off of a database, you can use CRUD (you'll need to modify the settings for CRUD if you're not using authentication, so that CRUD forms are accessible)

crud.create(db.some_table)

or

SQLFORM(db.some_table)

更多推荐

Web2py表单字段选项

本文发布于:2023-10-23 14:12:36,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   表单   选项   Web2py

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!