title: Django - Custom Filter to check if File exists
body: <p>I made this custom filter to check if an image exists or not:</p>
  
  <pre><code>from django import template
  from django.core.files.storage import default_storage
  
  register = template.Library()
  
  @register.filter(name='file_exists')
  def file_exists(filepath):
      if default_storage.exists(filepath):
          return filepath
      else:
          index = filepath.rfind('/')
          new_filepath = filepath[:index] + '/image.png'
          return new_filepath
  </code></pre>
  
  <p>And I used this in the template like this:</p>
  
  <pre><code>&lt;img src="{{ STATIC_URL }}images/{{ book.imageurl }}|file_exists" alt="{{book.title}} Cover Photo"&gt;
  </code></pre>
  
  <p>But it doesn't work. And I have no idea why.</p>