Django: guidelines for speeding up template rendering

How can I speed up the rendering of a Django template? My template takes about 1-2 seconds to render after the view function fully calculates everything it needs.

I already tried to perform all access to the database in the view, so that the template only gets into RAM, and not into the database engine.

I have a lot of include - maybe there is a problem there?

+10
source share
4 answers

I just spent a lot of time optimizing my django template code. Below are some optimization recommendations that worked for me, but depending on your setup, you may not get the same significant speedup.

  • Download the unicode template unicode , not str . Django forces all variables to unicode. This does not take too much time, but if the str variable is used in many places in your templates, it can turn into a noticeable delay. This is very easy to fix, whenever you send text data to Django's rendering, make sure it is unicode.
  • Mark variables as safe . Django's automatic security measures are really nice, but they come with a small punch with high performance. If you use many variables in your template, and know that it is safe, then be sure to check them as such.
  • Cache Compiled Templates . When you call render_template_from_string , django pulls out the template, compiles it, and then outputs it. Django simplifies the caching of the first two parts of this process and stores them in memory. All you have to do is make a small change to your settings.py file to add cached.Loader to your TEMPLATE_LOADERS . Read more about in the Django documentation .
  • Correct or not use endless pagination . I found that the endless pagination plugin slows down, This is because it has to load a new template for each page number. I went in and hacked it until I got the idea that I’m doing what I want, but before you do this, try removing it and see what quality of improvement you get.
  • Do not use the features of Fancy Templating . Inheritance, nested blocks, for loops and applications is great, but they have a cost of execution. Be very careful when using them and try not to perform multiple levels of inheritance. For loops, you can better handle client-side rendering.
  • Client side backup . An easy trick to speed up server-side processing is to simply do this on the client. One strategy is to embed the json structure in a django template, and then build the javascript HTML. This clearly defeats the django server side rendering goal, but it will lead to speedup. This is especially useful if you need to display content under a fold (that is, the user does not need to immediately see it when the page loads).

Doing the above reduces the rendering time for a complex page using the GAE example from about 1.0S to 250ms, but again, your mileage may vary.

+19
source

As a step 0, I would recommend adding an additional panel called Django Toolbar Template Sync to the Django debug toolbar . He told me exactly how much time was spent in each template (block, etc.) and how much time was taken up in SQL. This is also an additional check that this is your problem.

enter image description here

Here's how to add a panel to the debug panel. http://django-debug-toolbar.readthedocs.org/en/latest/configuration.html#debug-toolbar-panels

+9
source

Another trick I found: I needed to display an HTML table of 755 rows * 15 columns (filling out a total of 11 325 data).

This page used to delay loading by about 25/30 seconds, and then the page was a bit late. I set the table using display:none and after the page was fully loaded I changed the CSS property using JavaScript.

After all this, the page loads in a maximum of 6 seconds. I believe that Django spends much less time rendering invisible elements.

I don't know if this only works in my case, but it seems to be so.

+1
source

Use ManifestStaticFilesStorage to serve your static files. The performance CachedStaticFilesStorage that I have seen with using CachedStaticFilesStorage with the default LocMemCache are huge. The difference is that hashes never need to be computed at runtime.

I don’t quite understand why the difference is as great as it is now - although it is true that CachedStaticFilesStorage initially have to calculate hashes and fill the cache, after the cache is full, I do not expect a significant decrease in performance compared to the explicit method. But this is massive, and the documentation also recommends using ManifestStaticFilesStorage for performance.

0
source

All Articles