我正在尝试获取z3c form.Form以填充其信息,而不是在url中创建get参数,我想使用publishTraverse。
所以这是我的代码的一部分:
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)
在浏览器文件夹中的configure.zcml文件中:
<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>
当我在url中使用get参数时,我能够正常工作,但是当我尝试使用publishTraverse时,我发现找不到页面错误。什么是奇怪的是,什么时候
当我尝试使用发布遍历时,这就是我的url看起来的样子:
http://localhost:8190/MyPloneSite/@@myobject-editform/1
当我省略1,但保留“/”时,它仍然找到页面。我做错了什么导致了这个?
答案 0 :(得分:5)
除非您声明视图提供了IPublishTraverse接口,否则Zope发布者不会调用publishTraverse。您需要将其添加到您的班级:
from zope.publisher.interfaces.browser import IPublishTraverse
from zope.interface import implementer
@implementer(IPublishTraverse)
class EditMyObject(form.Form):
etc...
您还需要摆脱包装器视图。使用包装器,Zope遍历包装器,检查它是否提供IPublishTraverse,发现它没有,并放弃。相反,只需将表单直接注册为视图:
<browser:page
name="myobject-editform"
for="*"
permission="zope2.View"
class=".my_object_view.EditMyObject"
/>