python - Why does my custom 404 page return '404 ok' response in Django? -
i've render custom 404 template django. decided start on basic of it:
def custom_page_not_found(request): response = render_to_response('404.html', {}, context_instance=requestcontext(request)) response.status_code = 404 return response
and i'm pretty curious know why can't have "404 not found". if don't use handler404 urls.py, have blank 404 page current status. not when want have custom template.
does know why? (django 1.7.11)
in django 1.9+, changing status_code
(e.g. 404) change reason_phrase
if not set (e.g. 'not found'). however, using older version of django, have change reason_phrase
manually otherwise remain 'ok'.
it easier set status when creating response.
def custom_page_not_found(request): return render_to_response('404.html', {}, context_instance=requestcontext(request), status=404)
since render_to_response
shortcut obsolete, better use render
instead.
def custom_page_not_found(request): return render(request, '404.html', {}, status=404)
Comments
Post a Comment