2006-05-02 09:31:56 +08:00
|
|
|
from django import http
|
2011-08-12 22:15:31 +08:00
|
|
|
from django.template import (Context, RequestContext,
|
|
|
|
loader, TemplateDoesNotExist)
|
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
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
Templates: `404.html`
|
|
|
|
Context:
|
|
|
|
request_path
|
|
|
|
The path of the requested URL (e.g., '/app/pages/bad_page/')
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2007-02-27 01:43:41 +08:00
|
|
|
t = loader.get_template(template_name) # You need to create a 404.html template.
|
2007-06-22 15:15:04 +08:00
|
|
|
return http.HttpResponseNotFound(t.render(RequestContext(request, {'request_path': request.path})))
|
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
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
Templates: `500.html`
|
2005-07-13 09:25:57 +08:00
|
|
|
Context: None
|
|
|
|
"""
|
2007-02-27 01:43:41 +08:00
|
|
|
t = loader.get_template(template_name) # You need to create a 500.html template.
|
2007-06-22 15:15:04 +08:00
|
|
|
return http.HttpResponseServerError(t.render(Context({})))
|
2008-09-10 13:56:34 +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 %}.
|
|
|
|
@requires_csrf_token
|
|
|
|
def permission_denied(request, template_name='403.html'):
|
|
|
|
"""
|
|
|
|
Permission denied (403) handler.
|
|
|
|
|
|
|
|
Templates: `403.html`
|
|
|
|
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:
|
|
|
|
return http.HttpResponseForbidden('<h1>403 Forbidden</h1>')
|
|
|
|
return http.HttpResponseForbidden(template.render(RequestContext(request)))
|
|
|
|
|
|
|
|
|
2008-09-10 13:56:34 +08:00
|
|
|
def shortcut(request, content_type_id, object_id):
|
|
|
|
# TODO: Remove this in Django 2.0.
|
|
|
|
# This is a legacy view that depends on the contenttypes framework.
|
|
|
|
# The core logic was moved to django.contrib.contenttypes.views after
|
|
|
|
# Django 1.0, but this remains here for backwards compatibility.
|
|
|
|
# Note that the import is *within* this function, rather than being at
|
|
|
|
# module level, because we don't want to assume people have contenttypes
|
|
|
|
# installed.
|
|
|
|
from django.contrib.contenttypes.views import shortcut as real_shortcut
|
|
|
|
return real_shortcut(request, content_type_id, object_id)
|