python - Pylons:如何编写自定义 404 页面?

标签 python pylons http-status-code-404

所以这个问题has been asked before ,但没有详细回答。

我想覆盖默认的 Pylons 错误页面以制作一个更好的自定义页面。我已经覆盖了 error.py 中的 Controller ,如下所示:

def document(self):
    """Render the error document"""
    resp = request.environ.get('pylons.original_response')
    content = literal(resp.body) or cgi.escape(request.GET.get('message', ''))
    custom_error_template = literal("""\
    # some brief HTML here
    """)
    page = custom_error_template % \
        dict(prefix=request.environ.get('SCRIPT_NAME', ''),
             code=cgi.escape(request.GET.get('code', str(resp.status_int))),
             message=content)
    return page

这工作正常。我现在想做的是使用templates目录下的模板,这样404页面就可以继承我常用的布局模板,CSS等。

(我知道这对于 500 错误是个坏主意 - 在我使用模板而不是文字之前,我会检查 error.py 代码是 404。)

那么,问题来了。如何定义 custom_error_template 以指向我的模板,而不是文字?

最佳答案

您应该能够使用render 方法(从yourapp.lib.base 导入它,并使用return render('/path/to/error/模板')

关于python - Pylons:如何编写自定义 404 页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4449336/

相关文章:

python - 是否有必要在 tensorflow InteractiveSession() 之后关闭 session

python - 如何使用 PyGithub 在 repo 中创建一个新目录?

python - 将嵌套字典转换为附加数据框

python - Pylons 1.0 和 SQLAlchemy 0.6 - 如何建模?

javascript - PHP 应用程序出现无法解释的 404 错误

asp.net - 如何创建自定义 404 页面并发送 404 消息?

python - pandas 返回未命名的列

python - 关闭单元测试的翻译

pylons - 有没有人成功地将 celery 与 Pylons 一起使用

spring - 如何在自定义 Tomcat 404 页面上填充动态信息?