Opened 6 years ago

Closed 6 years ago

#29562 closed New feature (wontfix)

Allow the cache template tag to take a function that adds context data

Reported by: HongWeipeng Owned by: nobody
Component: Template system Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by HongWeipeng)

There is a problem in using template fragment caching, that is, we need transfer data for template every time, even the data has been in cache.

If the data acquisition process is time consuming (such as 3 seconds), template caching has no helpful.

My idea is to allow provide a callback function to tell the cache how to get the data. and only call the function when cache expires.

Although custom tags can be used to get the data in the cache, custom tags don't seem to handle html code very well.

I have tried it many times and found out how to make the simplest changes to complete this feature and ensure its compatibility.

Can you see this RP please? https://github.com/django/django/pull/10176

Change History (2)

comment:1 by HongWeipeng, 6 years ago

Description: modified (diff)

comment:2 by Tim Graham, 6 years ago

Component: Core (Cache system)Template system
Resolution: wontfix
Status: newclosed
Summary: An idea for improving the template fragment cacheAllow the cache template tag to take a function that adds context data

I think the proposed functionality is clever but it's too magical for my liking. Allowing the template tag's "fragment name" to be a function whose return value, if a dictionary, is added to the context looks like it wouldn't help template readability.

Also, I don't think the enhancement is in line with the philosophy of the Django template language, "the template system is meant to express presentation, not program logic." What I would probably do is write a custom get_data template tag and have that also take care of the caching. Your template tag can call template.render(). See TicketClosingReasons/UseSupportChannels for places to get help if needed.

If you disagree with my assessment, you can write to the DevelopersMailingList to get other opinions.

Note: See TracTickets for help on using tickets.
Back to Top