使用 mako 模板处理 404 错误
尝试在 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)
Trying to display template rendered by mako on 404 errors, but it still displays standart error page with cherrypy footer and additional message: |In addition, the custom error page failed: TypeError: render_body() takes exactly 1 argument (3 given)"
The code:
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})
Need help! How to display completely custom error pages with my layout(mako template)?
Full code:
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您很可能在 404 处理程序中引发错误,并且我猜您没有像 this,以及关于
response_body
的错误检查这个,您可能使用了错误的 模板的正文。从评论中编辑:
render
方法,仅使用关键字参数指定函数行为,您也可以更灵活一点并指定相同的函数,如下所示:It will make it easy to Expand your模板的参数,我通常使用
**args
用于我的
render
调用。但基本上问题是(正如您所指出的),您使用非关键字参数调用 render,而预期的输入只是模板的关键字参数。
You are most likely raising an error with in your 404 handler and I guess you not setting the
request.error_response
of the cherrypy config like this, and about the error ofresponse_body
check this, you are probably using wrong the body of the template.Edit from the comments:
The
render
method, only specify the function behavior with the keyword arguments, you could also be a little more flexible and specify the same function like this:It will make it easier to expand your arguments for the template, I usually use
**args
for my
render
calls.But the basically the problem was (as you pointed out), that you where calling render with non-keyword arguments, and the expected input is just keyword arguments, for the template.
所以,我想通了:)感谢cyraxjoe!这是代码:
So, I figured out :) Thanks to cyraxjoe! Here is the code: