2006-05-02 09:31:56 +08:00
|
|
|
from django import http
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.template import Context, Engine, TemplateDoesNotExist, loader
|
2015-04-22 03:54:00 +08:00
|
|
|
from django.utils import six
|
|
|
|
from django.utils.encoding import force_text
|
2010-10-28 19:47:15 +08:00
|
|
|
from django.views.decorators.csrf import requires_csrf_token
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2015-11-08 00:24:07 +08:00
|
|
|
ERROR_404_TEMPLATE_NAME = '404.html'
|
|
|
|
ERROR_403_TEMPLATE_NAME = '403.html'
|
|
|
|
ERROR_400_TEMPLATE_NAME = '400.html'
|
|
|
|
ERROR_500_TEMPLATE_NAME = '500.html'
|
|
|
|
|
2010-10-28 19:47:15 +08:00
|
|
|
|
2011-08-12 22:15:31 +08:00
|
|
|
# This can be called when CsrfViewMiddleware.process_view has not run,
|
|
|
|
# therefore need @requires_csrf_token in case the template needs
|
|
|
|
# {% csrf_token %}.
|
2010-10-28 19:47:15 +08:00
|
|
|
@requires_csrf_token
|
2015-11-08 00:24:07 +08:00
|
|
|
def page_not_found(request, exception, template_name=ERROR_404_TEMPLATE_NAME):
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2008-06-16 11:41:03 +08:00
|
|
|
Default 404 handler.
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2012-06-27 09:17:15 +08:00
|
|
|
Templates: :template:`404.html`
|
2006-05-02 09:31:56 +08:00
|
|
|
Context:
|
|
|
|
request_path
|
|
|
|
The path of the requested URL (e.g., '/app/pages/bad_page/')
|
2015-04-22 03:54:00 +08:00
|
|
|
exception
|
|
|
|
The message from the exception which triggered the 404 (if one was
|
|
|
|
supplied), or the exception class name
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2015-04-22 03:54:00 +08:00
|
|
|
exception_repr = exception.__class__.__name__
|
|
|
|
# Try to get an "interesting" exception message, if any (and not the ugly
|
|
|
|
# Resolver404 dictionary)
|
|
|
|
try:
|
|
|
|
message = exception.args[0]
|
|
|
|
except (AttributeError, IndexError):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if isinstance(message, six.text_type):
|
|
|
|
exception_repr = message
|
|
|
|
context = {
|
|
|
|
'request_path': request.path,
|
|
|
|
'exception': exception_repr,
|
|
|
|
}
|
2012-10-01 05:16:14 +08:00
|
|
|
try:
|
|
|
|
template = loader.get_template(template_name)
|
2015-01-08 22:03:43 +08:00
|
|
|
body = template.render(context, request)
|
2013-08-04 17:01:01 +08:00
|
|
|
content_type = None # Django will use DEFAULT_CONTENT_TYPE
|
2012-10-01 05:16:14 +08:00
|
|
|
except TemplateDoesNotExist:
|
2015-11-08 00:24:07 +08:00
|
|
|
if template_name != ERROR_404_TEMPLATE_NAME:
|
|
|
|
# Reraise if it's a missing custom template.
|
|
|
|
raise
|
2015-01-08 22:03:43 +08:00
|
|
|
template = Engine().from_string(
|
2012-10-01 05:16:14 +08:00
|
|
|
'<h1>Not Found</h1>'
|
|
|
|
'<p>The requested URL {{ request_path }} was not found on this server.</p>')
|
2015-01-08 22:03:43 +08:00
|
|
|
body = template.render(Context(context))
|
2013-08-04 17:01:01 +08:00
|
|
|
content_type = 'text/html'
|
|
|
|
return http.HttpResponseNotFound(body, content_type=content_type)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2010-10-28 19:47:15 +08:00
|
|
|
|
|
|
|
@requires_csrf_token
|
2015-11-08 00:24:07 +08:00
|
|
|
def server_error(request, template_name=ERROR_500_TEMPLATE_NAME):
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2005-11-11 12:45:05 +08:00
|
|
|
500 error handler.
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2012-06-27 09:17:15 +08:00
|
|
|
Templates: :template:`500.html`
|
2005-07-13 09:25:57 +08:00
|
|
|
Context: None
|
|
|
|
"""
|
2012-10-01 05:16:14 +08:00
|
|
|
try:
|
|
|
|
template = loader.get_template(template_name)
|
|
|
|
except TemplateDoesNotExist:
|
2015-11-08 00:24:07 +08:00
|
|
|
if template_name != ERROR_500_TEMPLATE_NAME:
|
|
|
|
# Reraise if it's a missing custom template.
|
|
|
|
raise
|
2013-08-04 17:01:01 +08:00
|
|
|
return http.HttpResponseServerError('<h1>Server Error (500)</h1>', content_type='text/html')
|
2015-01-08 22:03:43 +08:00
|
|
|
return http.HttpResponseServerError(template.render())
|
2008-09-10 13:56:34 +08:00
|
|
|
|
2010-10-28 19:47:15 +08:00
|
|
|
|
2013-05-16 07:14:28 +08:00
|
|
|
@requires_csrf_token
|
2015-11-08 00:24:07 +08:00
|
|
|
def bad_request(request, exception, template_name=ERROR_400_TEMPLATE_NAME):
|
2013-05-16 07:14:28 +08:00
|
|
|
"""
|
|
|
|
400 error handler.
|
|
|
|
|
|
|
|
Templates: :template:`400.html`
|
|
|
|
Context: None
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
template = loader.get_template(template_name)
|
|
|
|
except TemplateDoesNotExist:
|
2015-11-08 00:24:07 +08:00
|
|
|
if template_name != ERROR_400_TEMPLATE_NAME:
|
|
|
|
# Reraise if it's a missing custom template.
|
|
|
|
raise
|
2013-08-04 17:01:01 +08:00
|
|
|
return http.HttpResponseBadRequest('<h1>Bad Request (400)</h1>', content_type='text/html')
|
2015-04-22 03:54:00 +08:00
|
|
|
# No exception content is passed to the template, to not disclose any sensitive information.
|
2015-01-08 22:03:43 +08:00
|
|
|
return http.HttpResponseBadRequest(template.render())
|
2013-05-16 07:14:28 +08:00
|
|
|
|
|
|
|
|
2011-08-12 22:15:31 +08:00
|
|
|
# This can be called when CsrfViewMiddleware.process_view has not run,
|
|
|
|
# therefore need @requires_csrf_token in case the template needs
|
|
|
|
# {% csrf_token %}.
|
|
|
|
@requires_csrf_token
|
2015-11-08 00:24:07 +08:00
|
|
|
def permission_denied(request, exception, template_name=ERROR_403_TEMPLATE_NAME):
|
2011-08-12 22:15:31 +08:00
|
|
|
"""
|
|
|
|
Permission denied (403) handler.
|
|
|
|
|
2012-06-27 09:17:15 +08:00
|
|
|
Templates: :template:`403.html`
|
2011-08-12 22:15:31 +08:00
|
|
|
Context: None
|
|
|
|
|
|
|
|
If the template does not exist, an Http403 response containing the text
|
2016-05-02 20:35:05 +08:00
|
|
|
"403 Forbidden" (as per RFC 7231) will be returned.
|
2011-08-12 22:15:31 +08:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
template = loader.get_template(template_name)
|
|
|
|
except TemplateDoesNotExist:
|
2015-11-08 00:24:07 +08:00
|
|
|
if template_name != ERROR_403_TEMPLATE_NAME:
|
|
|
|
# Reraise if it's a missing custom template.
|
|
|
|
raise
|
2013-08-04 17:01:01 +08:00
|
|
|
return http.HttpResponseForbidden('<h1>403 Forbidden</h1>', content_type='text/html')
|
2015-04-22 03:54:00 +08:00
|
|
|
return http.HttpResponseForbidden(
|
|
|
|
template.render(request=request, context={'exception': force_text(exception)})
|
|
|
|
)
|