How to cache template in inclusion_tag

How to cache template in inclusion_tag



I am trying to cache whole template in inclusion_tag but I've got problem with that.



Here is my code of inclusion_tag:


@register.inclusion_tag('accounts/helpers/user_info.html', takes_context=True)
def user_info(context, username, size=40):
request = context['request']

user = cache.get(username)

if not user:
user = User.objects.get(pk=username)
print("cached")
cache.set(
username,
user,
settings.USER_LAST_ACTIVITY_TIMEOUT)

return 'user': user, 'size': size



and code of html template:


% load static i18n humanize accounts cache %
% get_current_language as LANGUAGE_CODE %

% cache 3600 user_info user.pk LANGUAGE_CODE size %
<a class="user-info" href=" user.get_absolute_url ">
<div class="user-avatar-wrapper">
<span class="online-status user.online_status "></span>
% if size %
% avatar user size class="avatar" id="user_avatar" %
% else %
% avatar user 40 class="avatar" id="user_avatar" %
% endif %
</div>
<span>
user
<small> user.groups.all</small>
</span>
</a>
% endcache %



With this code I cache only user but I want to cache whole template in tag. Could you help me how to do it ? I've tried everything but I do not have idea how to deal with that.



THANK YOU





I am sorry to say this but you are really wasting your time. Here. These sort of trivial queries should not be cached. Caching template rendering does not speed them up either.
– e4c5
Aug 29 at 11:34





User.objects.get(pk=username) you are fetching a single item using a primary key this will be just as fast as getting it from memcached.
– e4c5
Aug 29 at 11:35


User.objects.get(pk=username)





And if I want to.... How to do it ? Just let me know. Thank you :))
– Radovan Šanta
Aug 29 at 18:52






Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ