使用 mako 模板处理 404 错误

发布于 2024-12-07 17:51:12 字数 1724 浏览 0 评论 0原文

尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百善笑为先 2024-12-14 17:51:12

您很可能在 404 处理程序中引发错误,并且我猜您没有像 this,以及关于response_body的错误检查这个,您可能使用了错误的 模板的正文

从评论中编辑:

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)

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 of response_body check this, you are probably using wrong the body of the template.

Edit from the comments:

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})

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:

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

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.

千鲤 2024-12-14 17:51:12

所以,我想通了:)感谢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)

So, I figured out :) Thanks to cyraxjoe! Here is the 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:
    _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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文