如何修改 updateWidgets 中的 z3c 表单字段?
我正在尝试根据 HTTP get 变量中包含的变量动态更新表单字段。具体来说是www.site.com/form?id=name。我想从 url 中提取“id”字段并自动填充表单的 memberID 字段。
我可以使用 self.request.get('id') 访问 get 变量。但是我一直无法弄清楚如何更新表单字段。我检查了小部件文档 http://packages.python.org/z3c.form/widget。 html 但所有建议都不起作用。
class IUpdateEmailFormSchema(interface.Interface):
# -*- extra stuff goes here -*-
"""
"""
memberID = schema.TextLine(title=(u'Member id'))
email = schema.TextLine(title=(u'Email'), description=(u'The email'))
class updateEmailForm(form.Form):
fields = field.Fields(IUpdateEmailFormSchema)
label = _(u'Change Email')
ignoreContext = True
@button.buttonAndHandler(u'Update')
def handleUpdate(self,action):
data, errors = self.extractData()
if data.has_key('email'):
portal_membership = getToolByName(self.context, 'portal_membership')
member = portal_membership.getMemberById(data['memberID'])
def updateWidgets(self):
print "This is update widget \n\n"
import pdb; pdb.set_trace()
print self.request.form['id'] #Does not work as specified in http://packages.python.org/z3c.form/widget.html
#self.widgets["memberID"].value = self.request.get('id').encode('utf-8')
form.Form.updateWidgets(self)
updateEmailFormView = wrap_form(updateEmailForm)
*更新:updateWidget 未正确缩进。
I am trying to dynamically update a form field based on a variable that is included in a HTTP get variable. Specifically www.site.com/form?id=name. I want to pull the 'id' field from the url and automatically populate the memberID field of the form.
I have access to the get variable using self.request.get('id'). However I haven't been able to figure out how to update the form field. I examined the widget documentation http://packages.python.org/z3c.form/widget.html but none of the suggestions have worked.
class IUpdateEmailFormSchema(interface.Interface):
# -*- extra stuff goes here -*-
"""
"""
memberID = schema.TextLine(title=(u'Member id'))
email = schema.TextLine(title=(u'Email'), description=(u'The email'))
class updateEmailForm(form.Form):
fields = field.Fields(IUpdateEmailFormSchema)
label = _(u'Change Email')
ignoreContext = True
@button.buttonAndHandler(u'Update')
def handleUpdate(self,action):
data, errors = self.extractData()
if data.has_key('email'):
portal_membership = getToolByName(self.context, 'portal_membership')
member = portal_membership.getMemberById(data['memberID'])
def updateWidgets(self):
print "This is update widget \n\n"
import pdb; pdb.set_trace()
print self.request.form['id'] #Does not work as specified in http://packages.python.org/z3c.form/widget.html
#self.widgets["memberID"].value = self.request.get('id').encode('utf-8')
form.Form.updateWidgets(self)
updateEmailFormView = wrap_form(updateEmailForm)
*Update: updateWidget wasn't correctly indented.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
updateWidgets 方法是执行此操作的正确位置。
只是你应该首先调用“form.Form.updateWidgets”,然后执行你的操作
更改(甚至更好的用户 super() 如下所示):
所以基本上错误是顺序
,如果没有请求(这有点奇怪,你可能在那里做一些磨损的事情)那么你可以得到它 通过:
updateWidgets method is the right place to do it.
just you shouuld first call "form.Form.updateWidgets" and then do your
changes (even better user super() as shown bellow):
so basically the error was the order
and if there is no request (which is kinda weird and you are probably doing something worng there) then you can get it via:
您的 updateWigets() 不会在上面被调用,因为它不在表单主体内,它是一个顶级函数。
另外,您应该使用 super() 来调用 updateWidgets() 的父版本 - 在您的情况下,您可能希望首先调用父版本,然后更改值或其他内容。
您可能最好也使用默认值适配器而不是 updateWidgets() 。
Your updateWigets() won't be called above because it's not inside the body of the form, it's a top level function.
Also, you should use super() to call the parent version of updateWidgets() - in your case, you probably want to call the parent version first and then change the value or whatever.
You may be better off using a default value adapter instead of updateWidgets() too.