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

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

问题描述

我如何去加快Django模板渲染?我的模板需要大约1-2秒左右的时间才能渲染,视图函数完全可以计算出所需的任何内容。



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



我确实有很多 include s - 可能有问题吗?

解决方案

我刚刚花了大量的时间来优化我的django模板代码。以下是适用于我的优化指南,但根据您的设置,您可能无法获得显着的加速。




  • 将模板引擎 unicode ,而不是 str :Django将所有变量强制为unicode。这不需要太长的时间,但是如果您的模板中的许多地方使用了 str 变量,则可能会导致明显的延迟。当您将文本数据发送给Django渲染器时,请确保它是unicode。

  • 将变量标记为 safe :Django的自动安全措施是非常好的,但它们的性能受到很大的打击。如果您在模板中使用了许多变量,并且您知道它是安全的,那么请务必将其标记为这样。

  • 缓存编译模板:当你调用 render_template_from_string ,django拉模板,编译它,然后渲染它。 Django可以轻松地缓存此进程的前两部分,并将其存储在内存中。所有您需要做的是在 settings.py 文件中进行一个小的更改,将 cached.Loader 添加到您的 TEMPLATE_LOADERS 在Django文档中有更多解释。 li>
  • 修复或不使用无尽分页 :我发现无尽的分页插件非常缓慢的事情。这是因为它必须为每个页面编号加载一个新的模板。我进入并入侵黑客,直到我做到了我想要的,但在你这样做之前,尝试删除它,看看你获得什么样的性能改进。

  • Don不要使用花式模板功能:继承,嵌套块,for循环和包含很好,但它们具有性能成本。使用它们时要小心,尽量不要做多级继承。对于循环可能会更好地处理客户端渲染。

  • 客户端渲染:加快服务器端渲染的一个简单的技巧是在客户端上执行。一个策略是将json结构嵌入到django模板中,然后使用javascript构建HTML。这显然违反了django服务器端渲染的目的,但会导致加速。如果您必须将内容呈现在折叠下方(即,用户不需要在页面加载时立即看到它),这一点尤其有用。



上述操作将GAE实例上复杂页面的渲染时间从大约1.0S减少到250ms,但是再次,您的里程数可能有所不同。


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.

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

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

解决方案

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.

  • 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).

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天全站免登陆