Skip to content

Commit

Permalink
Safe way to use thumbnails in case file no longer exists
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmwatson committed Dec 12, 2024
1 parent a5a84de commit c030704
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pombola/core/templatetags/safe_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django import template

register = template.Library()

@register.filter
def safe_image_width(image):
if image and hasattr(image, 'width'):
return image.width
return None
5 changes: 5 additions & 0 deletions pombola/south_africa/templates/core/organisation_people.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{% load pagination_tags %}
{% load staticfiles %}
{% load thumbnail %}
{% load safe_image %}


{% block title %}{{ object.name }} People{% endblock %}
Expand Down Expand Up @@ -45,7 +46,11 @@ <h3>Show</h3>

<a href="{{ person.get_absolute_url }}">
{% thumbnail person.primary_image "58x78" crop="center" as im %}
{% if im|safe_image_width %}
<img src="{{ im.url }}" alt="{{ person.name }}" width="{{ im.width }}" height="{{ im.height }}" />
{% else %}
<img src="{% static 'images/person-90x90.jpg' %}" height="58" width="58"/>
{% endif %}
{% empty %}
<img src="{% static 'images/person-90x90.jpg' %}" height="58" width="58"/>
{% endthumbnail %}
Expand Down

0 comments on commit c030704

Please sign in to comment.