python 在django中使用Mako模板

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 在django中使用Mako模板相关的知识,希望对你有一定的参考价值。

from django.shortcuts import render

def index(request):
    return render(request, "test_app//index.html", {
        "msg" : "hello!!"
    })
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',
        },
    },
]
it works!! ${msg}
# 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)
        

以上是关于python 在django中使用Mako模板的主要内容,如果未能解决你的问题,请参考以下文章

Mako

Django 与 Mako Jinja2 模板比较的集成使用:render_to_response。问题?

Python模板库Mako的语法

删除 Mako 模板中的前导空格

mako 与 jinja2有啥区别

将 mako 渲染模板写入文件时创建的附加回车