Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8793277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:06:08+00:00 2026-06-13T23:06:08+00:00

In a Django template I have a pinterest style feed, and at the bottom

  • 0

In a Django template I have a pinterest style feed, and at the bottom of each block there’s an avatar pic.

I’m using the following template tag to show the avatar pic:

{% avatar user 40 %}

def avatar(user, size=80):
    if not isinstance(user, User):
        try:
            user = User.objects.get(username=user)
            alt = unicode(user)
            url = avatar_url(user, size)
        except User.DoesNotExist:
            url = AVATAR_DEFAULT_URL
            alt = _("Default Avatar")
    else:
        alt = unicode(user)
        url = avatar_url(user, size)
    picpath= """<img src="%s" alt="%s" width="%s" height="%s" />""" % (url, alt,
            size, size)
    return picpath

The problem is that this templatetag makes a call to the database each time to pull out the path to the avatar pic.

Basically what I want to do is only make this call the minimum number of times required (ie. extract the unique users in the current view and only get those pics once)

Is there a way to do this in the template? Or would I have to basically change my view in order to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:06:09+00:00Added an answer on June 13, 2026 at 11:06 pm

    You probably want to use a cache. You can use template fragment caching to cache the call to the avatar tag:

    {% load cache %}
    {# Cache for 60 seconds; you can use any value you want here #}
    {% cache 60 avatars %}
      {% avatar user 40 %}
    {% endcache %}
    

    You can also do caching in the view: either the whole view, or just cache your results. But template caching is probably easiest.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the regroup tag in a Django template to list a number of
In my django template I have some Event items listed using a for loop.The
I have the following code in django.template: class Template(object): def __init__(self, template_string, origin=None, name='<Unknown
How can I achieve this using the Django template system: Say I have 2
say I have the following list that I provide to a django template stuff=
I have the following code in my Django template: <a href={% url myapp.views.myview foobar
I have a problem using css in my django template, In my settings.py i
I'm using a template tag in a Django template (the tag is thumbnail )
I have the following for loop in my django template displaying days. I wonder,
In my Django template I have a POST form. It includes the tag {%

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.