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

发布于 2024-10-10 23:16:38 字数 1913 浏览 0 评论 0原文

我正在使用 Django 来构建我的网络项目。众所周知,mako 和 Jinja2 模板比 Django 给出的模板更快,我开始寻找使用 Django 的 render_to_response 方法将 mako 和 Jinja2 集成到 Django 中的方法。经过大量研究,我终于找到了实现这一目标的方法。然而,在我的集成中,jmeter的性能类似于Jinja2(3ms)> Django的模板(50ms)>鲭鱼(218 毫秒)。

如果我做错了什么?...或者请帮助建议一些集成 jinja2 和 mako 的最佳实践。

下面是编码 ()

Mako2django.py

from django.http import HttpResponse
from django.template import Context
from mako.lookup import TemplateLookup
from mysite.settings import TEMPLATE_DIRS 

   def render_to_mako(t,c=None,context_instance=None):
   path = TEMPLATE_DIRS
   makolookup = TemplateLookup(directories=[path],output_encoding='utf-   8',input_encoding='utf-8')
mako_temp = makolookup.get_template(t)
if context_instance:
    context_instance.update(c)
else:
    context_instance = Context(c)
data = {}
for d in context_instance:data.update(d)
return HttpResponse(mako_temp.render(**data))

Jinja2django.py

from django.http import HttpResponse
from django.conf import settings
from jinja2 import Environment, ChoiceLoader, FileSystemLoader

# Setup environment
default_mimetype = getattr(settings, 'DEFAULT_CONTENT_TYPE')

# Create the Jinja2 Environment
   env = Environment(
   line_statement_prefix='%',
   line_comment_prefix='##',
   loader=ChoiceLoader([FileSystemLoader(path) for path in getattr(settings,       'TEMPLATE_DIRS', ())]))

def render_to_string(filename, context={}):
   return env.get_template(filename).render(**context)

def render_to_jinja2(filename, context={},mimetype=default_mimetype, request = None):
   if request: context['request'] = request
   return HttpResponse(render_to_string(filename, context),mimetype=mimetype)

view.py 类似如下

from draft.jinja2django import render_to_jinja2

def view1(request):
    b = "helloworld"
    return render_to_jinja2('testjinja.html', context={"test":b})

I am using Django to build up my web project. As known that mako and Jinja2 template are faster that the one Django's given, I start finding way to integrate mako and Jinja2 into Django with using the Django's way of render_to_response method. After a lot research, I finally figure out the way to make this happen. However, in my integration, the jmeter's performance is something like Jinja2 (3ms) > Django's template (50ms) > mako (218ms).

If I went wrong with something?....Or pls help to advise some best practice to integrate jinja2 and mako.

Below is the coding ()

Mako2django.py

from django.http import HttpResponse
from django.template import Context
from mako.lookup import TemplateLookup
from mysite.settings import TEMPLATE_DIRS 

   def render_to_mako(t,c=None,context_instance=None):
   path = TEMPLATE_DIRS
   makolookup = TemplateLookup(directories=[path],output_encoding='utf-   8',input_encoding='utf-8')
mako_temp = makolookup.get_template(t)
if context_instance:
    context_instance.update(c)
else:
    context_instance = Context(c)
data = {}
for d in context_instance:data.update(d)
return HttpResponse(mako_temp.render(**data))

Jinja2django.py

from django.http import HttpResponse
from django.conf import settings
from jinja2 import Environment, ChoiceLoader, FileSystemLoader

# Setup environment
default_mimetype = getattr(settings, 'DEFAULT_CONTENT_TYPE')

# Create the Jinja2 Environment
   env = Environment(
   line_statement_prefix='%',
   line_comment_prefix='##',
   loader=ChoiceLoader([FileSystemLoader(path) for path in getattr(settings,       'TEMPLATE_DIRS', ())]))

def render_to_string(filename, context={}):
   return env.get_template(filename).render(**context)

def render_to_jinja2(filename, context={},mimetype=default_mimetype, request = None):
   if request: context['request'] = request
   return HttpResponse(render_to_string(filename, context),mimetype=mimetype)

The view.py is similar as below

from draft.jinja2django import render_to_jinja2

def view1(request):
    b = "helloworld"
    return render_to_jinja2('testjinja.html', context={"test":b})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蛮可爱 2024-10-17 23:16:38

从 Django 1.2 开始,您可以创建返回 Template 对象的自定义模板加载器。这样做,您可以使 django 的 render_to_response、render_to_string 和对应项使用您的模板系统进行渲染。

我正在使用这个:
https://gist.github.com/972162

它透明地加载 Jinja2 模板并回退到 Django 模板管理、贡献和第三方应用程序。

Starting from Django 1.2 you can create your custom template loader that returns your Template object. Doing that you can make django's render_to_response, render_to_string and counterparts render using your template system.

I am using this one:
https://gist.github.com/972162

It loads Jinja2 templates transparently and falls back to Django templates for admin, contrib and third-party apps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文