Zope 2: How to properly “browser:page” to make a p

2019-08-19 05:12发布

Let's assume I have a sample application with following classes:

##
## impelementation/SampleApp.py
##
class SampleApp: # inherits from required classes
    """ """
    implements(ISampleApp)
    # ...
    index_html = PageTemplateFile('templates/index.pt')


##
## implementation/SampleAppUser.py
##
class SampleAppUser: # inherits from required classes
    """ """
    implements(ISampleAppUser)
    # ...
    index_html = PageTemplateFile('templates/user_index.pt')

manage_addSampleAppUserForm = PageTemplateFile('manage_addSampleUserUserForm.pt')

Now I'd like to able to link to sign-in form (manage_addSampleUserUserForm.pt) from anywhere in the application, including its index page (index.pt).

I've tried the following approach but it doesn't work:

index.pt:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns="http://xml.zope.org/namespaces/tal">
  <head><title>SampleApp</title></head>
  <body>
     <h2>Title</h2><hr/>
     <a href="#" title="Sign In"
         tal:attributes="href here/signin.html">Sign In</a>
  </body>
</html>

configure.zcml:

<configure
    xmlns="http://namespaces.zope.org/zope"
    xmlns:five="http://namespaces.zope.org/five"
       xmlns:browser="http://namespaces.zope.org/browser">

  <five:registerPackage package="." initialize=".initialize" />

  <browser:page
          name="signin.html"
          class="bahmanm.sampleapp.implementation.SampleAppUser.SampleAppUser"
          template="templates/manage_addSampleUserForm.pt"
          permission="zope.Public"
  />

</configure>

During instance startup, Zope complains:

ZopeXMLConfigurationError: File "/home/bahman/sampleapp/sampleapp.devistan/src/bahmanm/sampleapp/configure.zcml", line 10.2-15.5
    TypeError: page() takes at least 4 arguments (5 given)

I also tried adding for="*" but Zope poped up the following error when navigating to the application index page:

2013-01-08 15:43:26 ERROR Zope.SiteErrorLog 1357647206.080.27220840385 http://localhost:8080/sampleapp/index_html
Traceback (innermost last):
  Module ZPublisher.Publish, line 126, in publish
  Module ZPublisher.mapply, line 77, in mapply
  Module ZPublisher.Publish, line 46, in call_object
  Module Shared.DC.Scripts.Bindings, line 322, in __call__
  Module Shared.DC.Scripts.Bindings, line 359, in _bindAndExec
  Module Products.PageTemplates.PageTemplateFile, line 130, in _exec
  Module Products.PageTemplates.PageTemplate, line 79, in pt_render
  Module zope.pagetemplate.pagetemplate, line 113, in pt_render
  Module zope.tal.talinterpreter, line 271, in __call__
  Module zope.tal.talinterpreter, line 343, in interpret
  Module zope.tal.talinterpreter, line 405, in do_startTag
  Module zope.tal.talinterpreter, line 482, in attrAction_tal
  Module Products.PageTemplates.Expressions, line 225, in evaluateText
  Module zope.tales.tales, line 696, in evaluate
   - URL: index_html
   - Line 7, Column 3
   - Expression: <PathExpr standard:'here/signin.html'>
   - Names:
      {'container': <SampleApp at /sampleapp>,
       'context': <SampleApp at /sampleapp>,
       'default': <object object at 0x7fa1499cf4c0>,
       'here': <SampleApp at /sampleapp>,
       'loop': {},
       'nothing': None,
       'options': {'args': ()},
       'repeat': <Products.PageTemplates.Expressions.SafeMapping object at 0x7fa134030ba8>,
       'request': <HTTPRequest, URL=http://localhost:8080/sampleapp/index_html>,
       'root': <Application at >,
       'template': <PageTemplateFile at /sampleapp/index>,
       'user': <User 'admin'>}
  Module zope.tales.expressions, line 217, in __call__
  Module Products.PageTemplates.Expressions, line 147, in _eval
  Module zope.tales.expressions, line 124, in _eval
  Module Products.PageTemplates.Expressions, line 74, in boboAwareZopeTraverse
  Module OFS.Traversable, line 317, in restrictedTraverse
  Module OFS.Traversable, line 275, in unrestrictedTraverse
   - __traceback_info__: ([], 'signin.html')
  Module zope.component._api, line 120, in queryMultiAdapter
  Module zope.component.registry, line 238, in queryMultiAdapter
  Module zope.interface.adapter, line 532, in queryMultiAdapter
TypeError: __init__() takes at most 2 arguments (3 given)

What am I doing wrong? To be honest, I believe I'm doing many things wrong :-) as I'm just blind-shooting by reading Zope 3 apidoc and reading other products' code (like SilvaForum).

I'd really appreciate any hint/help. TIA,

标签: zope
1条回答
时光不老,我们不散
2楼-- · 2019-08-19 05:46

Browser pages are separate components altogether. They are not part of the target content type but a separate component altogether.

If all you need is a template (no view), just omit the class directive altogether:

<browser:page
    name="signin.html"
    for="*"
    template="templates/manage_addSampleUserForm.pt"
    permission="zope.Public"
    />

There is no need to create a PageTemplateFile instance manually for that.

It is not entirely clear what you want to do with your link in your template. `here/signin.html" does not look like a valid TALES expression. In plain Zope there are no easy tools to generate absolute URLs relative to a site root; perhaps you just want /signin.html and not use a TAL expression?

查看更多
登录 后发表回答