From 703099f02ba8a935685cb6b45e67bc1e62a85da0 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Fri, 24 May 2024 20:50:53 +0100 Subject: [PATCH] Use jinja2 for templating --- requirements.txt | 1 + .../core/{templates => jinja2}/content.html | 2 +- yamdl_playground/core/models.py | 7 +++++-- yamdl_playground/core/views.py | 2 +- yamdl_playground/settings.py | 11 ++++++++++- 5 files changed, 18 insertions(+), 5 deletions(-) rename yamdl_playground/core/{templates => jinja2}/content.html (80%) diff --git a/requirements.txt b/requirements.txt index d055c3e..97ebdef 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ gunicorn markdown chrono django-debug-toolbar +django-jinja diff --git a/yamdl_playground/core/templates/content.html b/yamdl_playground/core/jinja2/content.html similarity index 80% rename from yamdl_playground/core/templates/content.html rename to yamdl_playground/core/jinja2/content.html index dea49fe..aca4fa9 100644 --- a/yamdl_playground/core/templates/content.html +++ b/yamdl_playground/core/jinja2/content.html @@ -3,5 +3,5 @@ {{ page.toc.html|safe }} - {{ content }} + {{ content|safe }} diff --git a/yamdl_playground/core/models.py b/yamdl_playground/core/models.py index 7efa8bd..259201f 100644 --- a/yamdl_playground/core/models.py +++ b/yamdl_playground/core/models.py @@ -1,7 +1,8 @@ from django.db import models import markdown -from django.template import Template from django.utils.functional import cached_property +from django.template import engines +from django_jinja.backend import Origin class Tag(models.Model): __yamdl__ = True @@ -50,5 +51,7 @@ class Page(models.Model): @cached_property def content_template(self): if (cached_template := self._template_cache.get(self.slug)) is None: - cached_template = self._template_cache[self.slug] = Template(self.content, name=self.slug) + cached_template = self._template_cache[self.slug] = engines["jinja2"].from_string(self.content) + cached_template.origin = Origin(name=self.slug, template_name=self.slug) + cached_template.name = self.slug return cached_template diff --git a/yamdl_playground/core/views.py b/yamdl_playground/core/views.py index 2790566..f32944f 100644 --- a/yamdl_playground/core/views.py +++ b/yamdl_playground/core/views.py @@ -19,7 +19,7 @@ def content(request, slug): return render(request, "content.html", { "page": page, - "content": page.content_template.render(Context({"request": request, "page": page})) + "content": page.content_template.render({"request": request, "page": page}) }) cached_content = cache_page(600, cache="mem")(content) diff --git a/yamdl_playground/settings.py b/yamdl_playground/settings.py index c21fed2..0aae9a2 100644 --- a/yamdl_playground/settings.py +++ b/yamdl_playground/settings.py @@ -25,7 +25,7 @@ SECRET_KEY = 'django-insecure-13rey)pheg9^%jtworn_&3wl&$-l%40uza0!ijva+l$n5umuz3 # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True -ALLOWED_HOSTS = [] +ALLOWED_HOSTS = ["*"] # Application definition @@ -59,6 +59,15 @@ TEMPLATES = [ ], }, }, + { + 'BACKEND': 'django_jinja.jinja2.Jinja2', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'app_dirname': "jinja2", + "match_extension": ".html" + } + }, ] WSGI_APPLICATION = 'yamdl_playground.wsgi.application'