Generalmente non ci dovrebbero essere messaggi personalizzati in 404 errori bu se si desidera implementarlo è possibile farlo usando i middleware django.
Middleware
from django.http import Http404, HttpResponse
class Custom404Middleware(object):
def process_exception(self, request, exception):
if isinstance(exception, Http404):
# implement your custom logic. You can send
# http response with any template or message
# here. unicode(exception) will give the custom
# error message that was passed.
msg = unicode(exception)
return HttpResponse(msg, status=404)
middleware Impostazioni
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'college.middleware.Custom404Middleware',
# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
questo farà il trucco. Correggimi se faccio qualcosa di sbagliato. Spero che questo ti aiuti.
fonte
2013-05-12 08:53:28
Poiché si esegue l'override di 'handler404', si progetta' 404.html' e si usa 'raise Http404' – karthikr