Merge pull request #384 from ThomasWaldmann/simple-error-views

use error views without templating, fixes #365
This commit is contained in:
TW 2018-10-19 20:07:16 +02:00 committed by GitHub
commit c93daae899
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 51 additions and 44 deletions

View File

@ -1,7 +0,0 @@
{% extends "errorpage.html" %}
{% block error %}
<h1>400 Bad Request</h1>
This kind of request is not allowed here.
{% endblock %}

View File

@ -1,9 +0,0 @@
{% extends "errorpage.html" %}
{% block error %}
<h1>403 Forbidden</h1>
Sorry, this is not allowed (for you).
If you did not log in yet, try logging in.
{% endblock %}

View File

@ -1,7 +0,0 @@
{% extends "errorpage.html" %}
{% block error %}
<h1>404 Not found</h1>
Nothing to see here.
{% endblock %}

View File

@ -1,7 +0,0 @@
{% extends "errorpage.html" %}
{% block error %}
<h1>500 Internal Server Error</h1>
Sorry, we screwed up. Try again later. :(
{% endblock %}

View File

@ -1,14 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>{{ WWW_HOST }} - something went wrong...</title>
</head>
<body>
{% block error %}
{% endblock %}
<a href="{% url 'home' %}">Back to {{ WWW_HOST }}.</a>
</body>
</html>

View File

@ -2,11 +2,14 @@
top-level url dispatching
"""
import six
from django.conf import settings
from django.conf.urls import include, url
from django.contrib import admin
from django.contrib.auth import views as auth_views
from django.conf.urls.static import static
from django.http import HttpResponse
def remember_me_login(request, *args, **kw):
@ -35,3 +38,51 @@ if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
import debug_toolbar
urlpatterns += [url(r'^__debug__/', include(debug_toolbar.urls)), ]
# we have expensive context processors and do not want to invoke them for the
# http error views, so we must not use templates (nor the django default views).
def http_error(request, status, exception=None):
if exception is not None:
exception_repr = exception.__class__.__name__
# Try to get an "interesting" exception message:
try:
message = exception.args[0]
except (AttributeError, IndexError):
pass
else:
if isinstance(message, six.text_type):
exception_repr = message
else:
# we do not have an exception for 500
exception_repr = 'Server Error'
body = """\
<h1>%(exception)s (error %(status)d)</h1>
""" % dict(
exception=exception_repr,
status=status,
)
return HttpResponse(body, content_type='text/html', status=status)
def bad_request(request, exception, template_name=None):
return http_error(request, 400, exception)
def permission_denied(request, exception, template_name=None):
return http_error(request, 403, exception)
def page_not_found(request, exception, template_name=None):
return http_error(request, 404, exception)
def server_error(request, template_name=None):
return http_error(request, 500)
handler400 = 'nsupdate.urls.bad_request'
handler403 = 'nsupdate.urls.permission_denied'
handler404 = 'nsupdate.urls.page_not_found'
handler500 = 'nsupdate.urls.server_error'