Python 为什么我的自定义404页面返回';404正常';在Django的回应?
我必须用django呈现一个自定义的404模板。 我决定从它的基础开始:Python 为什么我的自定义404页面返回';404正常';在Django的回应?,python,django,Python,Django,我必须用django呈现一个自定义的404模板。 我决定从它的基础开始: def custom_page_not_found(request): response = render_to_response('404.html', {}, context_instance=RequestContext(request)) response.status_code = 404 return response 我很好
def custom_page_not_found(request):
response = render_to_response('404.html', {},
context_instance=RequestContext(request))
response.status_code = 404
return response
我很好奇为什么我找不到“404未找到”。如果我不使用UrLS.Py的HANDLR404,我将拥有当前状态的空白404页。
但当我想要自定义模板时就不行了
有人知道为什么吗?(django 1.7.11)在django 1.9+中,如果未设置(如“未找到”),则更改(如更改为404)将更改。但是,您使用的是较旧版本的Django,因此必须手动更改
reason\u短语
,否则它将保持为“ok”
在创建响应时设置状态会更容易
def custom_page_not_found(request):
return render_to_response('404.html', {},
context_instance=RequestContext(request),
status=404)
由于render\u to_response
快捷方式已过时,因此最好使用render
def custom_page_not_found(request):
return render(request, '404.html', {}, status=404)
如果您只想为404页面使用自定义模板,则无需设置
handler404
或创建自定义视图。只需将您的404.html
模板粘贴到模板目录中即可。谢谢您的评论。但是,如果我需要出于任何目的使用自定义视图,我就不能有一个适当的404notfound
http响应吗?当然可以。但是你的问题很让人困惑;没有“404 ok”这样的东西,你是说它会返回“200 ok”吗?伙计,你是我今天的阳光(伦敦天气)。我被困在上面太久了。非常感谢你!