Django partially caches view

I have a simple page with parts:

  • list of items taken from the database
  • heading.

The heading contains the usual "login form" or the name of the user who logged in. For all users, the “part of the elements” is the same, but if I cache the whole view, a different header (your own username or login form if you are not logged in) does not appear according to the state of the user. How can I cache the "items" part and leave a dynamic header? Thanks.

+4
source share
2 answers

Use fragment fragment cache . This allows you to cache only a template fragment with your list of elements:

{% load cache %} A header here {% cache 500 %} List of items here {% endcache %} 
+5
source

If you use the Django cache system and version 1.3, this seems to be very easy with caching template fragments . In fact, the version given in the documents suggests caching both parts of the page as separate fragments by entering the title in the registered user:

 {% load cache %} {% cache 500 header request.user.username %} .. header .. {% endcache %} {% cache 500 items %} .. items .. {% endcache %} 
+3
source

Source: https://habr.com/ru/post/1397374/


All Articles