[Grok-dev] Custom not found page

Svenn Helge Grindhaug Svenn.Helge.Grindhaug at bccs.uib.no
Mon Nov 16 09:28:37 EST 2009


On Monday November 16 2009 11:32:05 Leonardo Rochael Almeida wrote:
> On Sat, Nov 14, 2009 at 10:24, Wichert Akkerman <wichert at wiggy.net> wrote:
> > On 11/14/09 00:03 , Kevin Teague wrote:
> >> Well, since you just asked for a "hint" and I don't have the full
> >> answer :P
> >>
> >> You've got:
> >>
> >> metal:use-macro="context/@@mblosum/macros/standard"
> >>
> >> But in a INotFound view, the context isn't your Application object,
> >> but a NotFound object.
> >
> > I've always found that to be somewhat confusing behaviour. In a Zope 2
> > context I use an error view like this:
> >
> > class ErrorView(grok.View):
> >     grok.context(Exception)
> >     grok.name("index.html")
> >     grok.template("error")
> >
> >     def update(self):
> >         self.exception=aq_inner(self.context)
> >         self.context=aq_parent(self)
> >         log.exception("Error at %r", self.context)
> >
> >
> > [...]
> 
> Perhaps we should generalize a trick like this in Grok itself. Say, in
> a class like grok.ErrorView where grok.name() would already be set,
> and grok.context() would be checked to be an actual Exception
> subclass.

For the record this is how my code ended up looking like:

class NotFound(grok.View):
    grok.context(zope.publisher.interfaces.INotFound)
    grok.template('notfound')
    grok.name('index.html')
   
    def update(self):
        self.context = self.context.getObject()
        self.response.setStatus(404)
    
    
class Error(grok.View):
    grok.context(Exception)
    grok.template('error')
    grok.name('index.html')

    def update(self):
        self.message = self.context.message
        self.args = self.context.args
        self.context = self.context.__parent__

And this seems to work nicely. From Wicherts post I thought I could get off by 
just having the Error class, but that did not catch typos in the URL.

Thanks for the hints,

Svenn.


More information about the Grok-dev mailing list