[Twisted-web] Custom 404, 500 pages only work with stan docFactory, not xml

Cory Dodt corydodt at twistedmatrix.com
Thu Nov 11 13:03:09 MST 2004

Hash: SHA1

On the Wiki, I posted (quite some time ago) a working example of putting your
own 404 and 500 pages in place of the plain-jane defaults.  The example still
works, and is at:


- --

However, at the same time I wrote that, I discovered that I cannot satisfy my
own use case, which is custom error pages built with xml*, because of some bug
in how Nevow selects the page context to render.  I have tried to find a patch
for this myself, but it just isn't happening.  Too much about context is still
mysterious to me :-/

Compare the minimal example below to the wiki page above.  The wiki works,
this one does not.  When you visit OopsPage, you should see a link to take you
back to the root page.  Instead, in that spot you get an error message saying
render_lastLink can't be found.

Thanks for looking :-)

- --

from nevow import appserver, rend, loaders, tags as T, inevow
from twisted.application import service, internet

class The500Page(rend.Page):
~    __implements__ = (inevow.ICanHandleException,)
~    docFactory = loaders.xmlstr("""\
<html xmlns:n="http://nevow.com/ns/nevow/0.1">
~    <head>
~        <title>500 error</title>
~    </head>
~    <body>
~        <h1>Ouchie.  Server error.</h1>
~        <p>The server is down.</p>
~        <p>Ohhh, the  server.</p>
~        <p n:render="lastLink" />
~    </body>

~    def renderHTTP_exception(self, ctx, failure):
~        inevow.IRequest(ctx).setResponseCode(500)
~        self.renderHTTP(ctx)
~    def render_lastLink(ctx, data):
~        referer = inevow.IRequest(ctx).getHeader('referer')
~        if referer:
~            return T.p[T.a(href=referer)['This link'],
~                       ' will return you to the last page you were on.']
~        else:
~            return T.p['You seem to be hopelessly lost.']

the500page = The500Page()

class OopsPage(rend.Page):
~    def exception(self, ctx, data):
~        1/0 # cause an exception on purpose
~    docFactory = loaders.stan(exception)

class RootPage(rend.Page):
~    docFactory = loaders.stan(
~            T.html[T.head[T.title["Error Pages"]],
~                   T.body[T.h1["The Only Real Page"],
~                          T.a(href="oops")[
~                            "This link causes an exception!"],

~                            ]]
~                              )
~    def locateChild(self, ctx, segments):
~        ctx.remember(the500page, inevow.ICanHandleException)
~        return rend.Page.locateChild(self, ctx, segments)

application = service.Application("errorpages")
root = RootPage()
root.putChild('oops', OopsPage())
svc = internet.TCPServer(8080, appserver.NevowSite(root))
Version: GnuPG v1.2.1 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org


More information about the Twisted-web mailing list