試圖顯示由mako在404錯誤上呈現的模板,但它仍然顯示cherrypy footer和附加消息的標準錯誤頁面:|此外,自定義錯誤頁面失敗:TypeError:render_body()正好1參數(3給出)」 代碼:?使用mako模板處理404錯誤
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)
我的404.mako只是一個純html,沒有任何包括。 request.error_response?我不明白 - 如何在我的情況下設置它? – Zhlobopotam
我想通了!所以後'類Root'添加 _cp_config = { 'error_page.404':error_page_404} 然後修改'error_page_404'函數將此: DEF error_page_404(狀態,消息,回溯,版本): \t TMPL = TPL。 get_template(「404.mako」) \t return tmpl.render(stat = status,msg = message) cherrypy.config.update({'error_page.404':error_page_404}) 現在它的工作原理! – Zhlobopotam
@Zhlobopotam你可以用一個很好的格式化代碼做出答案嗎? –