1
Fork 0

Use jinja2 for templating

This commit is contained in:
Jake Howard 2024-05-24 20:50:53 +01:00
parent a84bae8f04
commit 703099f02b
Signed by: jake
GPG key ID: 57AFB45680EDD477
5 changed files with 18 additions and 5 deletions

View file

@ -4,3 +4,4 @@ gunicorn
markdown
chrono
django-debug-toolbar
django-jinja

View file

@ -3,5 +3,5 @@
</head>
<body>
{{ page.toc.html|safe }}
{{ content }}
{{ content|safe }}
</body>

View file

@ -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

View file

@ -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)

View file

@ -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'