[Zope3-Users] z3c, GET variables handling pattern

Marius Gedminas marius at gedmin.as
Thu Sep 11 16:47:58 EDT 2008


On Thu, Sep 11, 2008 at 03:19:08PM -0400, Daniel Fimiarz wrote:
> I am new to plone/zope. I am trying to use z3c forms with plone to fetch
> some data from RDBMS but I am having an issue with variables passed to edit
> forms using GET and POST variables.
> 
> >From what I read, the edit form needs to override the getContent function:
> 
> def getContent(self):
>                #get the id from the request object
>                personid = int(*self.request['entityid']*)

Well, the usual idiom is to access the object through traversal, so the
form can work with self.context.  However it can be made to work the way
you're trying to get it to work.

>                #use the id to look up the person
>                person = sql.getPerson(personid)
> 
>                #create a return a dictionary
>                content = dict(
>                        [ (name, getattr(person,name))
>                        for name in self.fields.keys()])
>                return content

I haven't used z3c.form that much, but I'm think you could just
return the person object here instead of creating a dict.

> This works and I can display values just fine, but when I do something
> similar in the action handler
> 
> @button.buttonAndHandler(u'Apply',name='applyView')
>        def handleApplyView(self,action):
>                self.handleApply(self,action)
>                if not self.widgets.errors:
>                        url = absoluteURL(self.context,self.request)
>                        url += '/person-view?id=' + *self.request['entityid']
> *
>                        self.request.response.redirect(url)
> 
> 
> I get an error that there is no entityid key in self.request.  Inspecting
> the request object seems to confirm that.

To get it you'd need to add a hidden field to your form, e.g. in the
page template

    <input type="hidden" name="entityid" tal:attributes="value
    request/entityid" />

That would mean overriding the default form template, if you're not
doing that already.

> I guess my other options are to:
> 
> - Get the value from form directly with
> *self.request.form.widgets.entityid*(seems like an ugly thing to do)
> - Get the value from *data[]* dictionary.

Won't work.

> What would be the best pattern for handling this type of processing?
> 
> I have used z3c.formdemo and their example (sqlmessage) but for some reason
> self.request['id'] seems to work for them.

That's because they do exactly what I suggested: override the page
template and add a hidden field.  Take a look at
z3c/formdemo/sqlmessage/edit.pt

HTH,
Marius Gedminas
-- 
... there is always a well-known solution to every human problem -- neat,
plausible, and wrong.
		-- H. L. Mencken (1880-1956), "Prejudices"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mail.zope.org/pipermail/zope3-users/attachments/20080911/437e53ef/attachment.bin 


More information about the Zope3-users mailing list