I'm trying to get a z3c form.Form to have its information filled out, and rather than making a get parameter in the url, I would like to use publishTraverse.
So here is part of my code:
my_object_view.py:
class EditMyObject(form.Form):
fields = field.Fields(IMyObject)
ignoreContext = False
myObjectID = None
def publishTraverse(self, request, name):
print "Is this firing?"
if self.myObjectID is None:
self.myObjectID = name
return self
else:
raise NotFound()
def updateWidgets(self):
super(EditMyObject,self).updateWidgets()
#set id field's mode to hidden
def getContent(self):
db_utility = queryUtility(IMyObjectDBUtility, name="myObjectDBUtility")
return db_utility.session.query(MyObject).filter(MyObject.My_Object_ID==self.myObjectID).one()
#Button handlers for dealing with form also added
.....
from plone.z3cform.layout import wrap_form
EditMyObjectView = wrap_form(EditMyObject)
In my configure.zcml file in the browser folder:
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:five="http://namespaces.zope.org/five"
xmlns:genericsetup="http://namespaces.zope.org/genericsetup"
xmlns:zcml="http://namespaces.zope.org/zcml"
xmlns:browser="http://namespaces.zope.org/browser"
i18n_domain="my.object">
<browser:page
name="myobject-editform"
for="*"
permission="zope2.View"
class=".my_object_view.EditMyObjectView"
/>
</configure>
I was able to get it working when I was using the get parameter in the url, but when I try using publishTraverse, I get the page not found error. What is weird is that when
This is what my url looks like by the way when I try to use publish traverse:
http://localhost:8190/MyPloneSite/@@myobject-editform/1
When I leave out the 1, but keep the "/", it still finds the page. What am I doing wrong that causes this?
The Zope publisher won't call publishTraverse unless you declare that the view provides the IPublishTraverse interface. You need to add this to your class:
from zope.publisher.interfaces.browser import IPublishTraverse
from zope.interface import implementer
@implementer(IPublishTraverse)
class EditMyObject(form.Form):
etc...
You also need to get rid of your wrapper view. Using the wrapper, Zope traverses to the wrapper, checks to see if it provides IPublishTraverse, finds that it does not, and gives up. Instead, just register the form directly as a view:
<browser:page
name="myobject-editform"
for="*"
permission="zope2.View"
class=".my_object_view.EditMyObject"
/>