Jinja provides a function "render_"from_string, to use this we need to write a minimal Loader and hook this up to django.
django_jinja2.py
import jinja2
from django.template.loader import BaseLoader
from django.template import TemplateDoesNotExist
from yourproject.yourapp.models import DBTemplate
class JinjaDBLoader(BaseLoader):
is_usable = True
env = jinja2.Environment(
loader=jinja2.FileSystemLoader(settings.JINJA2_TEMPLATE_DIRS),
extensions=(
'jinja2.ext.i18n',
),
)
env.template_class = Template
def load_template(self, template_name, template_dirs=None):
# getting template/content from Database and cached it
object_manager = DBTemplate.objects.filter(name = template_name)
if object_manager.count() > 0:
# accessing cached object
return self.env.from_string( object_manager[0] ), None
else:
# if TemplateDoesNotExist is raised
# django go to the next Loader in TEMPLATE_LOADERS List
raise TemplateDoesNotExist( template_name )
Hook the Loader into settings.TEMPLATE_LOADERS link.
And use he default django render_to_response shortcut link
Keine Kommentare:
Kommentar veröffentlichen