【发布时间】:2010-12-21 05:06:45
【问题描述】:
我为经过身份验证的 Django 网站制作了一个自定义 handler404,以避免信息泄露。
def check_logged_in_404(request):
""" Custom 404. Show friendly 404 when logged in and redirect to /login
when not logged in.
"""
if request.user.is_authenticated():
return render_to_response('404.html')
else:
return HttpResponseRedirect('/login')
从功能上讲,它完全符合我的要求。但是,404 返回页面的状态为 200,这在代码方面是正确的。但这显然需要是404返回状态。
raise404 不起作用,因为如果不是以无限递归结束,它会回到这里并因此导致相同的问题。
我尝试了一个 HttpResponseNotFound,但这只接受一个字符串作为参数,而不是一个模板,这不是 DRY-ish。
我手动尝试设置标题:
response = render_to_response('404.html')
response['Status'] = "Not Found - 404"
return response
然后确实设置了状态标题,但浏览器仍然显示 200。
我别无选择..任何有提示的人,请成为我的英雄...:)
感谢和问候,
杰拉德。
编辑:我尝试了所有排序的状态字段值,但没有运气:(
【问题讨论】:
-
你无法相信这对我造成了多大的伤害
标签: django http-status-code-404