python - Pyramid :自定义 404 页面返回为 "200 OK"

标签 python pyramid

我在 Pyramid 应用程序中定义了一个自定义 404 View :

@view_config(context=HTTPNotFound, renderer='404.pt')
def not_found(self, request):
     return {}

它工作正常,除了与内容一起发送的 HTTP 状态码是 200 OK,这无论如何都不是 OK。我对 403 Forbidden 也有同样的问题。如何让 Pyramid 发送正确的状态码?

最佳答案

异常 View 是一个单独的 View ,它为您提供了一个可以做任何您想做的事情的地方。就像任何使用渲染器的 View 一样,您可以通过 request.response 影响响应对象以修改其行为。然后渲染器填充主体。

@view_config(context=HTTPNotFound, renderer='404.pt')
def not_found(self, request):
    request.response.status = 404
    return {}

关于python - Pyramid :自定义 404 页面返回为 "200 OK",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9815224/

相关文章:

Python 脚本未正确传递变量

python - 使用列表理解从列表列表中删除重复项

python - Pyramid_simpleform 可以与 python3.5.2 一起使用吗?

python - 使用 Pyramid、遍历和 PyMongo 更改模板

python - Pyramid:等同于 Pyramid/Python 中 PHP 框架中的 MVC?

python - 如何使用 `pip install git+ssh` 安装附加功能?

python - 如何在 Python 中定义子模块?

Python AES编码/解码字符串并存储在MySQL中

python - 维护用户输入数据,同时返回验证错误

python - 从 Python 调用 LibreOffice 时出错