使用mako模板处理404错误

时间:2011-10-03 07:32:59

标签: python cherrypy mako

尝试在404错误上显示由mako呈现的模板,但它仍然显示带有cherrypy页脚的标准错误页面和其他消息:|此外,自定义错误页面失败:TypeError:render_body()只取1个参数(给定3个)” 代码:

def error_page_404(status, message, traceback, version):
    tmpl = tpl.get_template("404.mako")
    return tmpl.render(status, message)
cherrypy.config.update({'error_page.404': error_page_404})

需要帮助!如何使用我的布局(mako模板)显示完全自定义的错误页面?

完整代码:

import sys
sys.stdout = sys.stderr
import os, atexit
import threading
import cherrypy
from mako.template import Template
from mako.lookup import TemplateLookup

cherrypy.config.update({'environment': 'embedded'})
if cherrypy.engine.state == 0:
    cherrypy.engine.start(blocking=False)
    atexit.register(cherrypy.engine.stop)

localDir = os.path.dirname(__file__)
absDir = os.path.join(os.getcwd(), localDir)
path = os.path.join(absDir,'files')
templ_path = os.path.join(absDir,'html')

tpl = TemplateLookup(directories=[templ_path], input_encoding='utf-8', output_encoding='utf-8',encoding_errors='replace')

def error_page_404(status, message, traceback, version):
    tmpl = tpl.get_template("404.mako")
    return tmpl.render(status, message)
cherrypy.config.update({'error_page.404': error_page_404})

class Root:
    def index(self):
    tmpl = tpl.get_template("index.mako")       
    return tmpl.render(text = 'Some text',url = cherrypy.url())
index.exposed = True    

_application = cherrypy.Application(Root(), None)

import posixpath

def application(environ, start_response):
    environ['SCRIPT_NAME'] = posixpath.dirname(environ['SCRIPT_NAME'])
    if environ['SCRIPT_NAME'] == '/':
        environ['SCRIPT_NAME'] = ''
        return _application(environ, start_response)

2 个答案:

答案 0 :(得分:2)

您最有可能在404处理程序中引发错误,我猜您没有像this那样设置cherrypy配置的request.error_response,以及response_body check {的错误{3}},您可能错误地使用了模板的this

从评论中编辑:

def error_page_404(status, message, traceback, version):
    tmpl = tpl.get_template("404.mako")
    return tmpl.render(stat=status, msg=message)

cherrypy.config.update({'error_page.404': error_page_404})

render方法,只使用关键字参数指定函数行为,您也可以更灵活一点,并指定相同的函数,如下所示:

def error_page_404(status, message, traceback, version):
    tmpl = tpl.get_template("404.mako")
    args = {'stat': status,
            'msg': message}
    return tmpl.render(**args)

这样可以更轻松地扩展模板的参数,我通常使用**args 我的render来电。

但基本上问题是(如你所指出的),你在哪里用非关键字参数调用render,而预期的输入只是模板的关键字参数。

答案 1 :(得分:1)

所以,我想通了:)感谢cyraxjoe!这是代码:

import sys
sys.stdout = sys.stderr
import os, atexit
import threading
import cherrypy
from mako.template import Template
from mako.lookup import TemplateLookup

cherrypy.config.update({'environment': 'embedded'})
if cherrypy.engine.state == 0:
    cherrypy.engine.start(blocking=False)
    atexit.register(cherrypy.engine.stop)

localDir = os.path.dirname(__file__)
absDir = os.path.join(os.getcwd(), localDir)
path = os.path.join(absDir,'files')
templ_path = os.path.join(absDir,'html')

tpl = TemplateLookup(directories=[templ_path], input_encoding='utf-8', output_encoding='utf-8',encoding_errors='replace')

def error_page_404(status, message, traceback, version):
    tmpl = tpl.get_template("404.mako")
    return tmpl.render(status, message)
cherrypy.config.update({'error_page.404': error_page_404})

class Root:
    _cp_config = {'error_page.404': error_page_404}
    def index(self):
    tmpl = tpl.get_template("index.mako")       
    return tmpl.render(text = 'Some text',url = cherrypy.url())
index.exposed = True    

_application = cherrypy.Application(Root(), None)

import posixpath

def application(environ, start_response):
    environ['SCRIPT_NAME'] = posixpath.dirname(environ['SCRIPT_NAME'])
    if environ['SCRIPT_NAME'] == '/':
        environ['SCRIPT_NAME'] = ''
        return _application(environ, start_response)