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
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.
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