From 33ba646433a3696b29bc57f107694457d6a20e2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20=27Necoro=27=20Neumann?= Date: Tue, 16 Mar 2010 23:41:07 +0100 Subject: Add a 404 page --- index.py | 27 +++++++++++++++++---------- templates/404.mako | 7 +++++++ 2 files changed, 24 insertions(+), 10 deletions(-) create mode 100644 templates/404.mako diff --git a/index.py b/index.py index a6775f3..eda595c 100755 --- a/index.py +++ b/index.py @@ -13,6 +13,8 @@ import helper APPDIR = os.path.dirname(os.path.abspath(__file__)) +app = web.auto_application() + def appdir (*args): return os.path.join(APPDIR, *args) @@ -24,31 +26,36 @@ class Renderer: output_encoding='utf-8', format_exceptions = True) - def render (self, tpl, **kwargs): + def render (self, tpl, level = "pages" , **kwargs): try: - t = self.get_tpl(tpl) + t = self.get_tpl(tpl, level) except mako.exceptions.TopLevelLookupException, e: - raise web.notfound(e) + raise app.notfound(tpl) return t.render(h = helper, url = helper.url, w = web, **kwargs) __call__ = render - def get_tpl (self, tpl): - return self.lookup.get_template(self.get_tpl_name(tpl)) + def get_tpl (self, tpl, level): + return self.lookup.get_template(self.get_tpl_name(tpl, level)) - def get_tpl_name (self, tpl): + def get_tpl_name (self, tpl, level): if not tpl.endswith(".mako"): tpl = tpl+".mako" - return os.path.join("pages", tpl) + return os.path.join(level, tpl) -urls = ("/(.*)", "Handler") -app = web.application(urls, globals()) +web.config.debug = True render = Renderer() -class Handler: +def FourOhFour(page): + return web.notfound(render("404", level = "", page = page)) + +app.notfound = FourOhFour + +class Handler (app.page): + path = "/(.*)" def GET(self, name = '/'): if not name or name == '/': name = 'index' return render(name) diff --git a/templates/404.mako b/templates/404.mako new file mode 100644 index 0000000..aeb95e3 --- /dev/null +++ b/templates/404.mako @@ -0,0 +1,7 @@ +<%! + title = "404 -- Page not found!" +%> +<%inherit file="/page.mako" /> +

+Sorry - the requested page ${page} cannot be found. +

-- cgit v1.2.3