Plone 4 表视图解决方法
我正在编写一个自定义视图来解决在 Plone 4 的表视图中显示错误的事件开始和结束时间的错误。但是,当我调用我的视图时,我收到以下错误:
回溯(最里面的最后):
模块 ZPublisher.Publish,第 116 行,发布
模块 ZPublisher.BaseRequest,第 498 行,遍历
模块 ZPublisher.BaseRequest,第 343 行,位于 traverseName
模块 ZPublisher.BaseRequest,第 110 行,publishTraverse
模块 zope.component._api,第 122 行,位于 queryMultiAdapter
模块 zope.component.registry,第 240 行,位于 queryMultiAdapter
模块 zope.interface.adapter,第 522 行,位于 queryMultiAdapter
类型错误:__init__
() 恰好需要 2 个参数(给定 3 个)
自从我创建视图以来已经有一段时间了,但我认为(根据 this) __init__()
确实需要 3 个参数(自我、上下文、请求)。无论如何,这就是我的 BrowserView 类目前的样子:
class NewEventsView(BrowserView):
"""Custom View for displaying events in a table with correct times"""
implements(INewEventsView)
def getCurrentEvents(self):
"""Method to return all active events in order"""
current_events = []
cat = getToolByName(self.context, 'portal_catalog')
brains = cat(portal_type='Event', review_state='published', sort_on='start')
for x in brains:
event = x.getObject()
if event.startDate > DateTime():
current_events.append(event)
return current_events
我尝试了添加 __init__
的不同变体,如上面提到的页面所示,只是为了给它一个__init__(self, context):
只是为了看看两个参数是否真的会改变任何东西,并且它给出了完全相同的错误。
我正在 Mac OS X Snow Leopard 上的 Plone 4.0.2 站点(在 python 2.6.6 virtualenv 中)中测试这个,
来自 browser/configure.zcml 的 BrowersView 注册(我将其放入我也在使用的主题中)。我调用 http://localhost:8080/plone/events/new_events_view 来查看视图的外观并得到上述错误。我还尝试在 Portal_types 中注册主题视图,并且在导航到 http://localhost 时会出现该错误:8080/plone/events 直到我删除视图。
<browser:page
for="*"
name="new_events_view"
class=".newEventsView.NewEventsView"
template="newEventsView.pt"
permission="zope.Public"
allowed_interface=".interfaces.INewEventsView"
/>
任何帮助将不胜感激。
另外,我知道这是一小段代码,但是如果您认为可以以更好的方式完成它,请将其拆开,我是一名学生,一直在寻找改进的方法。
谢谢
I'm writing a custom view to get around the bug that displays the wrong start and end times for events in table view in Plone 4. However, when I call my view I get the following error:
Traceback (innermost last):
Module ZPublisher.Publish, line 116, in publish
Module ZPublisher.BaseRequest, line 498, in traverse
Module ZPublisher.BaseRequest, line 343, in traverseName
Module ZPublisher.BaseRequest, line 110, in publishTraverse
Module zope.component._api, line 122, in queryMultiAdapter
Module zope.component.registry, line 240, in queryMultiAdapter
Module zope.interface.adapter, line 522, in queryMultiAdapter
TypeError:__init__
() takes exactly 2 arguments (3 given)
It's been awhile since I've created a view, but I thought (in accordance with this) that __init__()
does take 3 arguments (self, context, request). At any rate here's what my BrowserView class looks like at the moment:
class NewEventsView(BrowserView):
"""Custom View for displaying events in a table with correct times"""
implements(INewEventsView)
def getCurrentEvents(self):
"""Method to return all active events in order"""
current_events = []
cat = getToolByName(self.context, 'portal_catalog')
brains = cat(portal_type='Event', review_state='published', sort_on='start')
for x in brains:
event = x.getObject()
if event.startDate > DateTime():
current_events.append(event)
return current_events
I've tried different variations of this adding an __init__
as in the above mentioned page shows, and just for the heck of it giving it an __init__(self, context):
just to see if 2 arguments would really change anything and it gives the exact same error.
I'm testing this in a Plone 4.0.2 site on Mac OS X Snow Leopard (in a python 2.6.6 virtualenv)
BrowersView registration from browser/configure.zcml (I threw this into a theme I was also working with). I call http://localhost:8080/plone/events/new_events_view to see how the view looks and get the above error. I've also tried registering the view in portal_types for topic and it will give me that error upon navigating to http://localhost:8080/plone/events until I remove the view.
<browser:page
for="*"
name="new_events_view"
class=".newEventsView.NewEventsView"
template="newEventsView.pt"
permission="zope.Public"
allowed_interface=".interfaces.INewEventsView"
/>
Any help would be greatly appreciated.
Also, I know it's a small block of code, but rip it apart if you think it could be done in a better fashion, I'm a student always looking for ways to improve.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
无论如何,您都不应该在浏览器视图的
__init__
方法中放置任何代码。如果您想要一些自定义代码,请将其放入视图类的方法中或覆盖__call__
方法。该视图在安全上下文可用之前实例化,并且每个请求可能实例化多次。如果您在其中做了任何重要的工作,这可能会导致许多令人惊讶的效果 - 所以最好避免在那里做任何事情。在 Plone 4 中,您可以将自定义
__call__
编写为:You shouldn't put any code in the
__init__
method of a browser view anyways. If you want to have some custom code put it into methods on the view class or overwrite the__call__
method. The view is instantiated before a security context is available and might be instantiated multiple times per request. This can lead to a lot of surprising effects if you do any non-trivial work in it - so best just avoid doing anything in there.In Plone 4 you would write a custom
__call__
as:更好的解决方法是从 ATContentTypes 皮肤层自定义
formatCatalogMetadata.py
皮肤脚本。将行: 替换
为:
事件视图表再次对事件起作用。
A much better workaround would be to customize the
formatCatalogMetadata.py
skin script from the ATContentTypes skin layer.Replace the line reading:
with:
and the event view tables work again for events.
但它是python!
类方法的定义是:
默认情况下理解
self
(因为该方法被称为Class.__init__(context,request)
)(或者,再次缩短为类())。But it's python!
The definition for a class method is:
self
is understood by default (because the method is called asClass.__init__(context,request)
) (or, again, shortened toClass()
).