如何修改 updateWidgets 中的 z3c 表单字段?

发布于 2024-11-17 02:53:47 字数 1460 浏览 2 评论 0原文

我正在尝试根据 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

£冰雨忧蓝° 2024-11-24 02:53:47

updateWidgets 方法是执行此操作的正确位置。
只是你应该首先调用“form.Form.updateWidgets”,然后执行你的操作
更改(甚至更好的用户 super() 如下所示):

def updateWidgets(self):
    super(updateEmailForm, self).updateWidgets()
    id = self.request.get('id', None)
    if id:
        self.widgets["memberID"].value = id.encode('utf-8')

所以基本上错误是顺序

,如果没有请求(这有点奇怪,你可能在那里做一些磨损的事情)那么你可以得到它 通过

from zope.globalrequest import getRequest
request = getRequest()

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):

def updateWidgets(self):
    super(updateEmailForm, self).updateWidgets()
    id = self.request.get('id', None)
    if id:
        self.widgets["memberID"].value = id.encode('utf-8')

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:

from zope.globalrequest import getRequest
request = getRequest()
贪了杯 2024-11-24 02:53:47

您的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文