我必须使用django渲染自定义404模板。我决定从基础开始:
def custom_page_not_found(request):
response = render_to_response('404.html', {},
context_instance=RequestContext(request))
response.status_code = 404
return response
而且我很好奇为什么我找不到“ 404 not found”。如果不使用urls.py中的handler404,则将显示空白的404页面,其中包含当前状态。但是,当我要使用自定义模板时不可以。
有人知道为什么吗?(Django 1.7.11)
在Django 1.9+,改变status_code
(如404)将改变reason_phrase
,如果没有设置(例如,以“未找到”)。但是,你正在使用的Django的是旧版本,所以你就必须改变reason_phrase
手动否则它将继续作为“OK”。
创建响应时,更容易设置状态。
def custom_page_not_found(request):
return render_to_response('404.html', {},
context_instance=RequestContext(request),
status=404)
由于render_to_response
快捷方式已经过时,这将是更好地使用render
来代替。
def custom_page_not_found(request):
return render(request, '404.html', {}, status=404)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句