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 = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"DIRS": ["babybuddy/templates/error"],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [

View File

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

View File

@ -1,29 +1,21 @@
{% extends 'babybuddy/base.html' %}
{% load i18n widget_tweaks %}
{% extends 'error/base.html' %}
{% load i18n %}
{% block title %}403 {{ title }}{% endblock %}
{% 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">
<div>
<h1>403: {{ title }}</h1>
<div class="alert alert-danger" role="alert">
{{ main }} {{ reason }}
</div>
<div class="jumbotron">
<h2>{% trans "How to Fix" %}</h2>
{% blocktrans trimmed with origin=origin %}
Add <samp>{{ origin }}</samp> to the <code>CSRF_TRUSTED_ORIGINS</code>
environment variable. If multiple origins are required separate
with commas.
{% endblocktrans %}
</div>
</div>
</div>
</div>
{% block content %}
<h1>403: {{ title }}</h1>
<div class="alert alert-danger" role="alert">
{{ main }} {{ reason }}
</div>
<div class="jumbotron">
<h2>{% trans "How to Fix" %}</h2>
{% blocktrans trimmed with origin=origin %}
Add <samp>{{ origin }}</samp> to the <code>CSRF_TRUSTED_ORIGINS</code>
environment variable. If multiple origins are required separate
with commas.
{% endblocktrans %}
</div>
{% 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.middleware.csrf import REASON_BAD_ORIGIN
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.utils import translation
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
userful information about how to resolve the issue.
"""
print(reason == REASON_BAD_ORIGIN % request.META["HTTP_ORIGIN"])
if (
"HTTP_ORIGIN" in request.META
and reason == REASON_BAD_ORIGIN % request.META["HTTP_ORIGIN"]
):
c = {
ccontext = {
"title": _("Forbidden"),
"main": _("CSRF verification failed. Request aborted."),
"reason": reason,
"origin": request.META["HTTP_ORIGIN"],
}
try:
t = loader.get_template("error/403_csrf_bad_origin.html")
return HttpResponseForbidden(t.render(c), content_type="text/html")
except TemplateDoesNotExist:
pass
template = loader.get_template("error/403_csrf_bad_origin.html")
return HttpResponseForbidden(template.render(ccontext), content_type="text/html")
return csrf.csrf_failure(request, reason, "403_csrf.html")