django:加速模板渲染性能的指南 [英] django: guidelines for speeding up template rendering performance

查看:30
本文介绍了django:加速模板渲染性能的指南的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何加速 Django 模板渲染?我的模板大约需要 1-2 秒来渲染,在视图函数完全计算它需要的任何东西之后.

How would I go about speeding up Django template rendering? My template takes about 1-2 seconds or so to render, after the view function fully computes whatever it needs to.

我已经尝试在视图中执行所有数据库访问,这样模板只访问 RAM 而不是 DB 引擎.

I've already attempted to perform all database access in the view, such that the template only hits RAM and not the DB engine.

我确实有很多 include - 那里有问题吗?

I do have a lot of includes - could there be an issue there?

推荐答案

我刚刚花了很多时间优化我的 django 模板代码.以下是对我有用的优化指南,但根据您的设置,您可能不会获得显着的加速.

I just spent a good deal of time optimizing my django templating code. Below are optimization guidelines that worked for me, but depending on your setup, you may not get as significant of a speedup.

  • 输入模板引擎 unicode,而不是 str:Django 将所有变量强制转换为 unicode.这不会花费太长时间,但是如果 str 变量在您的模板中的许多地方使用,它可能会变成一个明显的延迟.这很容易修复,每当您将文本数据发送到 Django 渲染器时,请确保它是 unicode.
  • 将变量标记为safe:Django 的自动安全措施非常好,但它们确实对性能造成了很小的影响.如果您在模板中使用了许多变量,并且您知道它是安全的,那么请务必将其标记为安全.
  • 缓存已编译的模板:当您调用 render_template_from_string 时,django 会拉取模板,对其进行编译,然后进行渲染.Django 可以轻松缓存此过程的前两部分并将它们存储在内存中.您需要做的就是在您的 settings.py 文件中做一个小改动,将 cached.Loader 添加到您的 TEMPLATE_LOADERS 中.更多内容在在 Django 文档中进行了解释.莉>
  • 修复或不使用endless pagination:我发现无休止的分页 插件极大地减慢了速度.那是因为它必须为每个页码加载一个新模板.我深入研究了它,直到它达到我想要的效果为止,但在此之前,请尝试将其移除,看看您会获得何种类型的性能改进.
  • 不要使用花哨的模板功能:继承、嵌套块、for 循环和包含都很棒,但它们会带来性能成本.使用它们时要非常小心,尽量不要进行多级继承.使用客户端渲染可能会更好地处理 For 循环.
  • 客户端渲染:加速服务器端渲染的一个简单技巧就是在客户端上进行.一种策略是在 django 模板中嵌入一个 json 结构,然后让 javascript 构建 HTML.这显然违背了 django 服务器端渲染的目的,但会导致加速.如果您必须在折叠下方呈现内容(即,用户不需要在页面加载时立即看到它),这将特别有用.
  • Feed the Templating Engine unicode, not str: Django coerces all variables to unicode. This doesn't take too long to do, but if a str variable is being used in many places in your templates, it can turn into a noticeable delay. This is very easy to fix, whenever you're sending text data to a Django renderer, make sure it's unicode.
  • Mark Variables as safe: Django's automatic security measures are really nice, but they do come with a small performance hit. If you're using many variables in your template, and you know that it's safe, then be sure to mark it as such.
  • Cache Compiled Templates: When you call render_template_from_string, django pulls the template, compiles it, and then renders it. Django makes it easy to cache the first two parts of this process and store them in memory. All you need to do is make one small change in your settings.py file to add cached.Loader to your TEMPLATE_LOADERS. More is explained in the Django documentation.
  • Fix or Don't Use endless pagination: I found that the endless pagination plugin slowed things down extremely. That's because it has to load a new template for every single page number. I went in and hacked at it until I got it doing what I wanted, but before you do that, try removing it and see what type of performance improvement you get.
  • Don't Use Fancy Templating Features: Inheritance, nested blocks, for loops, and includes are great, but they come with a performance cost. Be very careful when using them and try not to do multiple levels of inheritance. For loops may be better handled with client side rendering.
  • Client Side Rendering: An easy trick to speed up server side rendering is to just do it on the client. One strategy is to embed a json structure in the django template, and then have javascript build the HTML. This obviously defeats the purpose of django server side rendering, but it will result in a speedup. This is especially useful if you have to render content below the fold (i.e., the user does not need to see it immediately when the page loads).

执行上述操作将我在 GAE 实例上的复杂页面的渲染时间从大约 1.0 秒缩短到 250 毫秒,但同样,您的里程可能会有所不同.

Doing the above cut my rendering time of a complex page on a GAE instance from about 1.0S to 250ms, but again, your mileage may vary.

这篇关于django:加速模板渲染性能的指南的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆