1

I set the local-memory cache "message" as shown below:

from django.core.cache import cache

cache.set("message", "success", 300)

Then, I tried to display it in the Django Template "index.html" as shown below:

# "index.html"

{{ cache.get.message }}

But, "success" is not displayed.

So, are there any ways to display the local-memory cache in Django Templates?

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129

1 Answers1

0

Django's template system is not simply Python embedded into HTML. It has a few wrappers built-in for looping, conditionally rendering, etc.

From Django Docs:

If you have a background in programming, or if you’re used to languages which mix programming code directly into HTML, you’ll want to bear in mind that the Django template system is not simply Python embedded into HTML. This is by design: the template system is meant to express presentation, not program logic.

In order to get a value from the cache you need to run cache.get('key_name') which is not allowed from within a template.

You can retrieve the value you want to display in the view and pass it to its template directly.

Mehrdad Moradi
  • 521
  • 1
  • 6
  • 21