>我正在尝试获取一个z3c表单。表单来填写其信息,而不是在 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 但保留"/"时,它仍然会找到页面。 我做错了什么导致这种情况?
Zope 发布者不会调用 publishTraverse,除非你声明该视图提供 IPublishTraverse 接口。 您需要将其添加到您的类中:
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"
/>