金字塔:自定义 404 页面返回“200 OK"; [英] Pyramid: Custom 404 page returns as "200 OK"

查看:70
本文介绍了金字塔:自定义 404 页面返回“200 OK";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Pyramid 应用中定义了一个自定义 404 视图:

I have a custom 404 view defined in my Pyramid app:

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

它工作正常,除了与内容一起发送的HTTP状态码是200 OK,这无论如何都不OK.我在使用 403 Forbidden 时遇到了同样的问题.如何让 Pyramid 发送正确的状态代码?

It works fine, except that the HTTP status code sent with the content is 200 OK, which is not OK by any means. I'm having the same problem with 403 Forbidden. How can I get Pyramid to send the correct status code?

推荐答案

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

The exception view is a separate view that provides a spot for you to do whatever you want. Just like any view that uses a renderer, you can affect the response object via request.response to modify its behavior. The renderer then fills in the body.

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

这篇关于金字塔:自定义 404 页面返回“200 OK";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆