Eric R.
Eric R.

Reputation: 81

Plone 4.3.x - grokcore.view - UserWarning: Found the following unassociated template after configuration

On a vanilla Plone 4.3.3 site (Unified Installer on Ubuntu 14.04.1LTS), and after updating buildout.cfg with the zopeskel and paster boiler plate stuff and running buildout, I successfully created a dexterity package in my src folder:

$ cd src  
$ ../bin/zopeskel dexterity my.package

After updating buildout.cfg (adding my.package to the eggs section and src/my.package to the develop section) and running buildout, I added content to my new package:

$ cd my.package  
$ ../../bin/paster addcontent dexterity_content

I called the new content type mytype, resulting in mytype.py, a templates folder called mytype_templates, etc.

Restarting Plone and.... so far, so good....

Then I add templates to the mytype_templates folder:

add.pt
edit.pt
view.pt

In the mytype.py file I added all the necessary imports, schema definition

Class Imytype(form.Schema, IImageScaleTraversable):
    ....
    ....

, etc, etc, and obviously also the view, add and edit classes:

class View(dexterity.DisplayForm):
    grok.context(Imytype)
    grok.require('zope2.View')

    # Disable turn fieldsets to tabs behavior
    enable_form_tabbing  = False

    def update(self):
        super(View, self).update()


class Add(dexterity.AddForm):
    grok.name('my.package.mytype')

    # Disable turn fieldsets to tabs behavior
    enable_form_tabbing  = False

    def __init__(self, context, request):
        super(Add, self).__init__(context, request)
    ......
    ......

class Edit(dexterity.EditForm):
    grok.context(Imytype)

    # Disable turn fieldsets to tabs behavior
    enable_form_tabbing  = False

    def update(self):
        super(Edit, self).update()
    ......
    ......

When I restart my Plone site in foreground mode, I get the following messages:

2015-02-06 12:52:41 INFO ZServer HTTP server started at Fri Feb  6 12:52:41 2015
Hostname: 0.0.0.0
Port: 8080
/home/Plone434_site/buildout-cache/eggs/grokcore.view-2.8-py2.7.egg/grokcore/view/templatereg.py:261: UserWarning: Found the following unassociated template after configuration: /home/Plone434_site/zinstance/src/my.package/my/package/mytype_templates/edit.pt
  warnings.warn(msg, UserWarning, 1)
/home/Plone434_site/buildout-cache/eggs/grokcore.view-2.8-py2.7.egg/grokcore/view/templatereg.py:261: UserWarning: Found the following unassociated template after configuration: /home/Plone434_site/zinstance/src/my.package/my/package/mytype_templates/add.pt
  warnings.warn(msg, UserWarning, 1)
2015-02-06 12:52:46 INFO Zope Ready to handle requests

Seemingly grok successfully picks up the view.pt, but not the add.pt and edit.pt
This is confirmed by customizing the templates. Changes to view.pt renders fine, changes to add.pt and edit.pt have no results. Plone falls back on the default dexterity templates, as the add.pt and edit.pt are not grokked.

I found a work-around by adding the following:

....
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
....

and to the Add class:

    template = ViewPageTemplateFile('mytype_templates/add.pt')

and to the Edit class:

    template = ViewPageTemplateFile('mytype_templates/edit.pt')

Obviously the error messages as listed above are still there, but at least it works and I can customize the add.pt and edit.pt. Although I can live with this workaround, I am wondering why only the view.pt is grokked and not the add.pt and edit.pt. Please notice that this (weird?) behavior was also duplicated using Plone 4.3.1, 4.3.2, 4.3.3 and 4.3.4

Any suggestions?

Upvotes: 2

Views: 135

Answers (1)

hvelarde
hvelarde

Reputation: 2876

You have to declare the name, context, layer and schema of the views; use something like this (note the grok.layer method which maybe you're missing):

class AddForm(dexterity.AddForm):
    grok.name('my.package.mytype')
    grok.layer(Imylayer)
    grok.context(Imytype)
    schema = Imytype

    def update(self):
        super(AddForm, self).update()
        ...


class EditForm(dexterity.EditForm):
    grok.context(Imytype)
    grok.layer(Imylayer)
    schema = Imytype

    def update(self):
        super(EditForm, self).update()
        ...

Alternatively you may skip the use of Grok at all and register everything via ZCML.

An example of this can be found in the collective.nitf package. There's a branch using Grok and a pull request removing it.

Upvotes: 1

Related Questions