Cache page templates
This commit is contained in:
parent
79aab1e15e
commit
eef673db2f
2 changed files with 12 additions and 4 deletions
|
@ -1,5 +1,7 @@
|
||||||
from django.db import models
|
from django.db import models
|
||||||
import markdown
|
import markdown
|
||||||
|
from django.template import Template
|
||||||
|
from django.utils.functional import cached_property
|
||||||
|
|
||||||
class Tag(models.Model):
|
class Tag(models.Model):
|
||||||
__yamdl__ = True
|
__yamdl__ = True
|
||||||
|
@ -12,6 +14,8 @@ class Tag(models.Model):
|
||||||
class Page(models.Model):
|
class Page(models.Model):
|
||||||
__yamdl__ = True
|
__yamdl__ = True
|
||||||
|
|
||||||
|
_template_cache = {}
|
||||||
|
|
||||||
title = models.CharField(max_length=255)
|
title = models.CharField(max_length=255)
|
||||||
|
|
||||||
raw_content = models.TextField()
|
raw_content = models.TextField()
|
||||||
|
@ -34,3 +38,9 @@ class Page(models.Model):
|
||||||
instance.tags.set(tags)
|
instance.tags.set(tags)
|
||||||
|
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
|
@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)
|
||||||
|
return cached_template
|
||||||
|
|
|
@ -2,7 +2,7 @@ from .models import Page
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from django.db import connections
|
from django.db import connections
|
||||||
from django.shortcuts import get_object_or_404, render
|
from django.shortcuts import get_object_or_404, render
|
||||||
from django.template import Template, Context
|
from django.template import Context
|
||||||
|
|
||||||
def search(request):
|
def search(request):
|
||||||
with connections["default"].cursor() as cursor:
|
with connections["default"].cursor() as cursor:
|
||||||
|
@ -15,9 +15,7 @@ def search(request):
|
||||||
def content(request, slug):
|
def content(request, slug):
|
||||||
page = get_object_or_404(Page, slug=slug)
|
page = get_object_or_404(Page, slug=slug)
|
||||||
|
|
||||||
template = Template(page.content)
|
|
||||||
|
|
||||||
return render(request, "content.html", {
|
return render(request, "content.html", {
|
||||||
"page": page,
|
"page": page,
|
||||||
"content": template.render(Context({"request": request, "page": page}))
|
"content": page.content_template.render(Context({"request": request, "page": page}))
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue