Last active
August 29, 2015 14:23
-
-
Save z4none/3192612cfbfa752f3888 to your computer and use it in GitHub Desktop.
using Mako template in django
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # coding = utf-8 | |
| from django.utils.module_loading import import_string | |
| from django.utils.functional import cached_property | |
| from django.template.engine import _dirs_undefined | |
| from django.template.backends.base import BaseEngine | |
| from django.template.backends.utils import csrf_input_lazy, csrf_token_lazy | |
| from django.template.context import _builtin_context_processors | |
| from mako.lookup import TemplateLookup | |
| class MakoTemplates(BaseEngine): | |
| app_dirname = 'templates' | |
| def __init__(self, params): | |
| params = params.copy() | |
| options = params.pop('OPTIONS').copy() | |
| super(MakoTemplates, self).__init__(params) | |
| self.context_processors = [] | |
| if 'context_processors' in options: | |
| self.context_processors = options.pop('context_processors') | |
| self.lookup = TemplateLookup(directories=self.template_dirs, **options) | |
| @cached_property | |
| def template_context_processors(self): | |
| context_processors = _builtin_context_processors | |
| context_processors += tuple(self.context_processors) | |
| return tuple(import_string(path) for path in context_processors) | |
| def from_string(self, template_code): | |
| return MakoTemplateWrapper(self, Template(template_code)) | |
| def get_template(self, template_name, dirs=_dirs_undefined): | |
| return MakoTemplateWrapper(self, self.lookup.get_template(template_name)) | |
| class ContextProcessorTemplate(object): | |
| def __init__(self, engine, template): | |
| self.engine = engine | |
| self.template = template | |
| def render(self, context=None, request=None): | |
| if context is None: | |
| context = {} | |
| for processor in self.engine.template_context_processors: | |
| context.update(processor(request)) | |
| return self.render_internal(context) | |
| class MakoTemplateWrapper(ContextProcessorTemplate): | |
| def render_internal(self, context): | |
| return self.template.render(**context) | |
| class Template(object): | |
| def __init__(self, template): | |
| self.template = template | |
| def render(self, context=None, request=None): | |
| if context is None: | |
| context = {} | |
| if request is not None: | |
| context['request'] = request | |
| context['csrf_input'] = csrf_input_lazy(request) | |
| context['csrf_token'] = csrf_token_lazy(request) | |
| return self.template.render(context) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| it works!! ${msg} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| TEMPLATES = [ | |
| { | |
| 'BACKEND': 'djangomako.MakoTemplates', | |
| 'DIRS': [os.path.join(BASE_DIR, 'templates')], | |
| 'APP_DIRS': True, | |
| 'OPTIONS': { | |
| 'context_processors': [ | |
| 'django.template.context_processors.debug', | |
| 'django.template.context_processors.request', | |
| 'django.template.context_processors.static', | |
| 'django.contrib.auth.context_processors.auth', | |
| 'django.contrib.messages.context_processors.messages', | |
| ], | |
| 'input_encoding': 'utf-8', | |
| 'output_encoding': 'utf-8', | |
| }, | |
| }, | |
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django.shortcuts import render | |
| def index(request): | |
| return render(request, "test_app//index.html", { | |
| "msg" : "hello!!" | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment