From 011c967446a0438ed21aba1fffe3c27269f6a14e Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Tue, 6 Jun 2023 21:32:41 +0200 Subject: [PATCH] errors should work but dont --- gawa/gawa/urls.py | 5 +++-- gawa/start/templates/errors/400.html | 2 +- gawa/start/templates/errors/403.html | 15 ++++++++++++++ gawa/start/views.py | 29 +++++++++++++++++++--------- 4 files changed, 39 insertions(+), 12 deletions(-) create mode 100644 gawa/start/templates/errors/403.html diff --git a/gawa/gawa/urls.py b/gawa/gawa/urls.py index 2acddd5..4c4b561 100644 --- a/gawa/gawa/urls.py +++ b/gawa/gawa/urls.py @@ -33,6 +33,7 @@ urlpatterns += i18n_patterns( # use my fancy error pages # better yet, don't let it come to that # FIXME these break the server. something is wrong with them -#handler400 = 'start.views.Error400' -#handler404 = 'start.views.Error404' +handler400 = 'start.views.Error400' +handler403 = 'start.views.Error403' +handler404 = 'start.views.Error404' #handler500 = 'start.views.Error500' diff --git a/gawa/start/templates/errors/400.html b/gawa/start/templates/errors/400.html index 8e58b3a..5d98112 100644 --- a/gawa/start/templates/errors/400.html +++ b/gawa/start/templates/errors/400.html @@ -5,7 +5,7 @@ {% block title %}{% translate "cscherr.de" %} - {% translate "startpage" %}{% endblock title %} {% block main %}
-

404 {% translate "Bad request" %}

+

400 {% translate "Bad request" %}

{% translate "You sent bad data to the server." %}

diff --git a/gawa/start/templates/errors/403.html b/gawa/start/templates/errors/403.html new file mode 100644 index 0000000..7e8e75e --- /dev/null +++ b/gawa/start/templates/errors/403.html @@ -0,0 +1,15 @@ +{% extends 'base.html' %} +{% load i18n %} +{% get_current_language as LANGUAGE_CODE %} +{% block languagecode %}{{ LANGUAGE_CODE }}{% endblock languagecode %} +{% block title %}{% translate "cscherr.de" %} - {% translate "startpage" %}{% endblock title %} +{% block main %} +
+

403 {% translate "Permission denied" %}

+

{% translate "You are not allowed to access this page." %}

+
+
+

{% translate "Looking for anything specific?" %}

+ {% include 'main_search_form.html' %} +
+{% endblock main %} diff --git a/gawa/start/views.py b/gawa/start/views.py index 8dfd2d7..dc1d222 100644 --- a/gawa/start/views.py +++ b/gawa/start/views.py @@ -1,3 +1,4 @@ +from django.http import HttpResponseBadRequest, HttpResponseForbidden, HttpResponseNotAllowed, HttpResponseNotFound, HttpResponseServerError from django.shortcuts import redirect from django.utils import translation from django.views.generic.base import View @@ -6,6 +7,7 @@ from django.views import View from django.shortcuts import render from django.views.generic.list import QuerySet from django.db.models import Q +from django.views.static import loader from requests import request from .forms import MainSearchForm @@ -110,10 +112,19 @@ def Error400(request, exception): """ template_name = "errors/400.html" - data = { - 'exception': exception - } - return render(request, template_name, data, status=400) + t = loader.get_template(template_name) + data = {'exception': exception} + return HttpResponseBadRequest(t.render(data, request), content_type='application/xhtml+xml') + +def Error403(request, exception): + """ + 403 View, the user has been denied permission + """ + + template_name = "errors/403.html" + t = loader.get_template(template_name) + data = {'exception': exception} + return HttpResponseForbidden(t.render(data, request), content_type='application/xhtml+xml') def Error404(request, exception): """ @@ -123,10 +134,9 @@ def Error404(request, exception): """ template_name = "errors/404.html" - data = { - 'exception': exception - } - return render(request, template_name, data, status=404) + t = loader.get_template(template_name) + data = {'exception': exception} + return HttpResponseNotFound(t.render(data, request), content_type='application/xhtml+xml') def Error500(request): """ @@ -136,6 +146,7 @@ def Error500(request): """ template_name = "errors/500.html" + t = loader.get_template(template_name) data = {} - return render(request, template_name, data, status=500) + return HttpResponseServerError(t.render(data, request), content_type='application/xhtml+xml')