1

I have a simple Django app with a single view for displaying the product list:

from time import sleep
<other imports>

class ProductListView(ListView):
    model = Product
    paginate_by = 50
    allow_empty = True

    def get(self, *args, **kwargs):
        sleep(2)
        return super().get(*args, **kwargs)

And the template:

<div class="container">
  {% for product in page_obj %}
    <div class="col-3 my-2">
       <product details>
      </div>
  {% endfor %}
</div>

I use sleep(2) to emulate the slow uploading of a huge amount of objects from the database.

Now I need to implement the next workflow:

  • template from this view should be cached. So the first page upload should take >2 sec, the next one <2sec, uploaded from the cache.
  • change Product item in the database (via admin page for example)
  • page on refresh is loaded from cache, but product info should be updated to the newest

I'm, researching the Django caching mechanism, but I'm not sure which one should I try. So is it possible in Django to implement this workflow?

Daniil
  • 51
  • 3
  • Does it really take 2 seconds to fetch 50 results from your DB? It shouldn't take that long, can you share the complete template? – Iain Shelvington Mar 29 '22 at 21:17
  • No, but I would like to implement a cache mehanism if it will. The full template contains a few more tags that is not useful for the question, i believe. – Daniil Mar 29 '22 at 21:40

0 Answers0