I'm using django-imagekit to resize my user avatars and right now to display a default avatar (if the user didn't upload his/her avatar) I do this:
views.py
try:
usr_avatar = UsrAvatar.objects.get(user=request.user.id)
except UsrAvatar.DoesNotExist:
usr_avatar = UsrAvatar.objects.get(id='0')
template.html
<img src="{{ usr_avatar.avatar_image.url }}" >
This works fine but every time a user didn't upload his/her avatar I'm hitting the database for the default avatar image.
Is there a way to eliminate hitting the database when the user doesn't have an avatar image loaded by somehow attributing the default image link to usr_avatar
or just doing something in the template.html? Thank you!
Apt username given your question!
You could create a context processor that provides the default avatar to every template and simply make sure that the context processor caches the image
settings.py
myapp/context_processors.py
Now the template variable 'default_avatar' is available in every template:
Alternatively just use the cache in your original query:
But Finally, it might be even better to avoid keeping the default avatar in the database at all and instead just write a context processor like above but instead of getting the default avatar from the DB, just have a static url to the image