2006-05-02 09:31:56 +08:00
|
|
|
from django import http
|
2015-02-10 02:19:34 +08:00
|
|
|
from django.template import Context, Engine, TemplateDoesNotExist, loader
|
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
|
|
|
|
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
|
2006-05-02 09:31:56 +08:00
|
|
|
def page_not_found(request, template_name='404.html'):
|
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/')
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2015-01-08 22:03:43 +08:00
|
|
|
context = {'request_path': request.path}
|
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-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
|
2006-05-02 09:31:56 +08:00
|
|
|
def server_error(request, template_name='500.html'):
|
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:
|
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
|
|
|
|
def bad_request(request, template_name='400.html'):
|
|
|
|
"""
|
|
|
|
400 error handler.
|
|
|
|
|
|
|
|
Templates: :template:`400.html`
|
|
|
|
Context: None
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
template = loader.get_template(template_name)
|
|
|
|
except TemplateDoesNotExist:
|
2013-08-04 17:01:01 +08:00
|
|
|
return http.HttpResponseBadRequest('<h1>Bad Request (400)</h1>', content_type='text/html')
|
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
|
|
|
|
def permission_denied(request, template_name='403.html'):
|
|
|
|
"""
|
|
|
|
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
|
|
|
|
"403 Forbidden" (as per RFC 2616) will be returned.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
template = loader.get_template(template_name)
|
|
|
|
except TemplateDoesNotExist:
|
2013-08-04 17:01:01 +08:00
|
|
|
return http.HttpResponseForbidden('<h1>403 Forbidden</h1>', content_type='text/html')
|
2015-01-08 22:03:43 +08:00
|
|
|
return http.HttpResponseForbidden(template.render(request=request))
|