Using z3x.form with Grok ======================== :Author: http://vimes656.myopenid.com/ Create a sample z3c.form project +++++++++++++++++++++++++++++++++ Create a sample application with grokproject: .. code-block:: bash $ grokproject z3cformsample Edit setup.py ++++++++++++++ Add the following packages: .. code-block:: python ... install_requires=['setuptools', 'grok', 'zope.viewlet', # <---- 'z3c.form', # <---- 'z3c.formui', # <---- 'z3c.macro', # <---- 'z3c.template', # <---- 'z3c.layer', # <---- ], ... Then run the buildout: .. code-block:: bash $ bin/buildout -N Now open the app.py file and add: .. code-block:: python import grok from zope.interface import Interface, implements from zope import schema from z3c.form import button, field, form, widget from z3c.formui import layout from z3c.formui import interfaces from z3c.form.interfaces import IFormLayer from z3c.layer.pagelet import IPageletBrowserLayer from persistent import Persistent class Z3cFormSampleLayer(IFormLayer, IPageletBrowserLayer): pass class AggregatedLayer(IDivFormLayer, Z3cFormSampleLayer): pass class Z3cFormSampleSkin(grok.Skin): grok.name('IDoubtIt') grok.layer(AggregatedLayer) ... Here we just created the skin 'IDoubtIt'. The skin is composed of 2 layers: Z3cFormSampleLayer and the AggregatedLayer which adds simple autogenerated Div widgets. The name 'IDoubtIt' will be the one will have to use for the ++skin++ namespace traversal. In the rest of the code will use the Grok layer. .. code-block:: python ... class Z3cformSample(grok.Application, grok.Container): grok.layer(AggregatedLayer) pass Now we register the layer for our which has nothing, just for simplicity sake. .. code-block:: python ... class Index(grok.View): grok.layer(AggregatedLayer) def items(self): return self.context.items() ... Again, for the Index view we have to register the Aggregated Layer. The items method returns the items in .. code-block:: python ... class IPet(Interface): name = schema.TextLine( title=u'name of the Pet', description=u'To call your pet use this name', required=True,) class Pet(Persistent): implements(IPet) name = schema.fieldproperty.FieldProperty(IPet['name']) ... By inheriting from Persistent we are ensuring that the objects instantiated from Something class are persisted in ZODB. This is for demo porpouses, for real applications better to use a contanier. ... class SomethingView(grok.View): grok.layer(AggregatedLayer) grok.context(Something) grok.name('index.html') def render(self): return '

I am lazy... create a better view for this

' class AddForm(form.AddForm, grok.View): grok.layer(AggregatedLayer) fields = field.Fields(ISomething) label = u'This is a add form' def create(self, data): return Something(**data) def add(self, object): count = 0 while 'something-%i' %count in self.context: count += 1; self._name = 'something-%i' %count self.context[self._name] = object return object def nextURL(self): return self.context[self._name].redirect('index.html') class EditForm(form.EditForm, grok.View): grok.context(Something) grok.layer(AggregatedLayer) grok.name('edit.html') form.extends(form.EditForm) label = u'This is a edit form' fields = field.Fields(ISomething) @button.buttonAndHandler(u'Apply and View', name='applyView') def handleApplyView(self, action): self.handleApply(self, action) if not self.widgets.errors: self.redirect('index.html') Run /bin/zopectl fg and point your browser to localhost:8080/