【问题标题】:web2py: customizing form.vars to pass inputs idweb2py:自定义 form.vars 以传递输入 id
【发布时间】:2018-04-01 22:25:23
【问题描述】:

我正在使用 web2py 为参加员工构建一个网络应用程序。

这是模型中的数据库

db.define_table(
    'employee',
     Field('name'),
     format = '%(name)s'
)
db.define_table(
    'attendance',
    Field('employee_id',db.employee),
    Field('attend','boolean'),
    Field('comments','text')
)

这是控制器:

employeeIDS=db(db.employee).select(db.employee.ALL)
table=[]
for eid in employeeIDS:  
table.append([eid.name,INPUT(_type="checkbox",_name=eid.id,_id=eid.id),INPUT(_type="text",_name="comments",_id=eid.id)])

form=FORM( TABLE(TR("employee name","attended?","comments"),*[TR(*rows) for rows in table]),INPUT(_type="submit",_value="SUBMIT"))    
if form.accepts(request,session):
     response.flash="form accepted"
     print(request.vars)
elif form.errors:
     response.flash="form is invalid"
else:
     response.flash="please fill the form"

return dict(form=form,vars=form.vars)

我的问题是:
如何访问每行中的参加和 cmets 字段的 ID 以将这些字段与相关员工相关联。所以,当我将 form.vars 插入考勤表时,我保证每个员工都记录为考勤或缺勤,并且相关的 cmets 也会插入。

谢谢

【问题讨论】:

    标签: forms django-forms customization web2py


    【解决方案1】:

    不要给每个 comments 输入相同的名称,而是使名称唯一,包括记录 ID 作为名称的一部分:

    INPUT(_type="text", _name="comments_%s" % eid.id)
    

    然后你会有form.vars.comments_1form.vars.comments_2等。

    顺便说一句,请注意 HTML id 属性应该是唯一的,但您将值 eid.id 用作复选框输入和 cmets 输入的 id 属性。

    【讨论】:

      【解决方案2】:

      我认为更好的选择是使用SQLFROM 而不是FORM。

      在那里你可以customize the resulting form in the view 对你想要的布局,即使有表格的 HTML 助手,你已经使用过。

      请查看signature of the SQLFORM - 它提供了很多标签或 showid 选项(点击链接向下滚动一点)。

      要排除正在更改甚至在 SQLFORM 中显示的字段,请使用:

      db.my_table.a_field.writable = False
      db.my_table.a_field.readable = False
      

      here 所述 - SQLFORMS 也是如此。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-09-30
        • 2019-12-12
        • 1970-01-01
        • 1970-01-01
        • 2018-04-18
        • 1970-01-01
        • 2016-04-30
        相关资源
        最近更新 更多