How do I pass a (hidden) parameter to the next page?

(Part of ZopeInAnger)

HTTP is a stateless protocol. One way of maintaining state is the use of user sessions - where the server keeps track of state through a continued set of variables on the server. See also How do I use cookie based sessions?. Sessions can be tricky though - especially when users have multiple browsers open and sessions can expire.

One trick is to pass parameters between webforms. If you have a sequence of forms that is often used. A hidden field/widget can be passed from one form to the next.

Formlib supports the hidden attribute for a widget. Create the widget at set it to the hidden state. Examples of use can be found in the ZOPE source unit tests in ./zope/app/form/browser/. Create a widget and display it:

print normalize( widget.hidden() )

Working outside the widget machinery you can also 'hard code' hidden attributes in a ZPT page. This is probably not the recommended way, but can be quite handy for a quick hack. For example:

<input class="textType" type="hidden" tal:attributes="id string:${view/PREFIX}.year; name string:${view/PREFIX}.year; value view/yearname" type="text" />

The PREFIX, in this case, matches the one formlib uses.

In both cases the hidden field is fetched through the request variable of the receiving form. For example:

class XBrowserView(BrowserView):

  def __init__(self, context, request):
    super(XBrowserView,self).__init__(context,request)
    year = self.request.get(self.PREFIX+'.year')
    # or (if it always exists):
    year = request[self.PREFIX+'.'+'year']

If you feel you can improve on this page please edit it!



( 97 subscribers )