Source

caktus-deployment / django-template / PROJECT_NAME / views.py

from django import http
from django.template import Context, RequestContext, loader


# in lieu of http://code.djangoproject.com/ticket/5617
def render_safe(request, template_name):
    """
    Try to render the given template using RequestContext.  If that fails,
    use an empty Context instead.
    """
    t = loader.get_template(template_name)
    try:
        context = RequestContext(request, {'request_path': request.path})
        return t.render(context)
    except:
        return t.render(Context({}))


def page_not_found(request, template_name='404.html'):
    """
    Default 404 handler.

    Templates: `404.html`
    Context:
        request_path
            The path of the requested URL (e.g., '/app/pages/bad_page/')
    """
    return http.HttpResponseNotFound(render_safe(request, template_name))


def server_error(request, template_name='500.html'):
    """
    500 error handler.

    Templates: `500.html`
    Context: None
    """
    return http.HttpResponseServerError(render_safe(request, template_name))