Create base error template

This commit is contained in:
Christopher C. Wells 2022-02-22 08:31:14 -08:00 committed by Christopher Charbonneau Wells
parent f49a54400e
commit 94d1f99344
6 changed files with 52 additions and 36 deletions

View File

@ -77,7 +77,7 @@ ROOT_URLCONF = "babybuddy.urls"
TEMPLATES = [ TEMPLATES = [
{ {
"BACKEND": "django.template.backends.django.DjangoTemplates", "BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [], "DIRS": ["babybuddy/templates/error"],
"APP_DIRS": True, "APP_DIRS": True,
"OPTIONS": { "OPTIONS": {
"context_processors": [ "context_processors": [

View File

@ -1,13 +1,10 @@
{% extends 'babybuddy/page.html' %} {% extends 'error/base.html' %}
{% load i18n widget_tweaks %} {% load i18n %}
{% block title %}403 {% trans "Permission Denied" %}{% endblock %} {% block title %}403 {% trans "Permission Denied" %}{% endblock %}
{% block breadcrumbs %}
<li class="breadcrumb-item active" aria-current="page">{% trans "Permission Denied" %}</li>
{% endblock %}
{% block content %} {% block content %}
<h1>403 {% trans "Permission Denied" %}</h1>
<div class="alert alert-danger" role="alert"> <div class="alert alert-danger" role="alert">
{% blocktrans trimmed %} {% blocktrans trimmed %}
You do not have permission to access this resource. Contact a site You do not have permission to access this resource. Contact a site

View File

@ -1,15 +1,11 @@
{% extends 'babybuddy/base.html' %} {% extends 'error/base.html' %}
{% load i18n widget_tweaks %} {% load i18n %}
{% block title %}403 {{ title }}{% endblock %} {% block title %}403 {{ title }}{% endblock %}
{% block breadcrumb_nav %}{% endblock %} {% block breadcrumb_nav %}{% endblock %}
{% block page %} {% block content %}
<div class="container mt-2 mt-lg-5">
<div class="row justify-content-md-center">
<div class="col-lg-12 mb-4">
<div>
<h1>403: {{ title }}</h1> <h1>403: {{ title }}</h1>
<div class="alert alert-danger" role="alert"> <div class="alert alert-danger" role="alert">
{{ main }} {{ reason }} {{ main }} {{ reason }}
@ -22,8 +18,4 @@
with commas. with commas.
{% endblocktrans %} {% endblocktrans %}
</div> </div>
</div>
</div>
</div>
</div>
{% endblock %} {% endblock %}

View File

@ -0,0 +1,13 @@
{% extends 'error/base.html' %}
{% load i18n %}
{% block title %}404 {% trans "Page Not Found" %}{% endblock %}
{% block content %}
<h1>404 {% trans "Page Not Found" %}</h1>
<div>
{% blocktrans trimmed with request_path=request_path %}
The path <samp>{{ request_path }}</samp> does not exist.
{% endblocktrans %}
</div>
{% endblock %}

View File

@ -0,0 +1,14 @@
{% extends 'babybuddy/base.html' %}
{% load i18n widget_tweaks %}
{% block breadcrumb_nav %}{% endblock %}
{% block page %}
<div class="container mt-2 mt-lg-5">
<div class="row justify-content-md-center">
<div class="col-lg-12 mb-4">
{% block content %}{% endblock %}
</div>
</div>
</div>
{% endblock %}

View File

@ -8,7 +8,7 @@ from django.contrib.messages.views import SuccessMessageMixin
from django.http import HttpResponseForbidden from django.http import HttpResponseForbidden
from django.middleware.csrf import REASON_BAD_ORIGIN from django.middleware.csrf import REASON_BAD_ORIGIN
from django.shortcuts import redirect, render from django.shortcuts import redirect, render
from django.template import loader, TemplateDoesNotExist from django.template import loader
from django.urls import reverse, reverse_lazy from django.urls import reverse, reverse_lazy
from django.utils import translation from django.utils import translation
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
@ -34,21 +34,21 @@ def csrf_failure(request, reason=""):
Overrides the 403 CSRF failure template for bad origins in order to provide more Overrides the 403 CSRF failure template for bad origins in order to provide more
userful information about how to resolve the issue. userful information about how to resolve the issue.
""" """
print(reason == REASON_BAD_ORIGIN % request.META["HTTP_ORIGIN"])
if ( if (
"HTTP_ORIGIN" in request.META "HTTP_ORIGIN" in request.META
and reason == REASON_BAD_ORIGIN % request.META["HTTP_ORIGIN"] and reason == REASON_BAD_ORIGIN % request.META["HTTP_ORIGIN"]
): ):
c = { ccontext = {
"title": _("Forbidden"), "title": _("Forbidden"),
"main": _("CSRF verification failed. Request aborted."), "main": _("CSRF verification failed. Request aborted."),
"reason": reason, "reason": reason,
"origin": request.META["HTTP_ORIGIN"], "origin": request.META["HTTP_ORIGIN"],
} }
try: template = loader.get_template("error/403_csrf_bad_origin.html")
t = loader.get_template("error/403_csrf_bad_origin.html") return HttpResponseForbidden(template.render(ccontext), content_type="text/html")
return HttpResponseForbidden(t.render(c), content_type="text/html")
except TemplateDoesNotExist:
pass
return csrf.csrf_failure(request, reason, "403_csrf.html") return csrf.csrf_failure(request, reason, "403_csrf.html")