【发布时间】:2011-11-29 17:42:21
【问题描述】:
尝试在 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)
【问题讨论】: