Plone/Zope/Z3c- 什么会导致 publishTraverse "not find the page?"

Plone/Zope/Z3c- What can cause publishTraverse to "not find the page?"

我正在尝试让 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 但保留“/”时,它仍能找到该页面。我做错了什么导致了这个?

除非您声明视图提供 IPublishTraverse 接口,否则 Zope 发布者不会调用 publishTraverse。您需要将此添加到您的 class:

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"
/>