Capturing WindowsError in template
<p>I'm coding on a Windows machine but I'm running my production site on Linux.</p>

<p>When trying to reach a page on my development machine using a database copied from the production system, I get errors while trying to list files if these files don't exist locally. 
This is as expected, since I just copied the DB and not the files. I don't want/need the files, but I don't want following error either:</p>

<blockquote>
  <p>WindowsError at /126/documents/ [Error 3] The system cannot find the
  path specified:
  u'C:\mysite\media\documents\2016\07\26\myfile.docx'</p>
</blockquote>

<p>Instead of throwing the error, I'd prefer to handle this in my template, something like:</p>

<pre><code>{% if doc.data %}{{ doc.data.size | filesizeformat }}{% else %}File not found{% endif %}
</code></pre>

<p>However that doesn't work. <code>doc.data</code> <em>does</em> exist, since the DB knows a value for this file location. But the file isn't available on the disk.</p>

<p>Any way to catch this properly, preferably in the template?</p>

<p>My model:</p>

<pre><code>class Document(models.Model):
    data = models.FileField(upload_to="documents/%Y/%m/%d")
</code></pre>

<p>Not the best answer, but a workaround can be implemented as follows in <code>views.py</code> (not in the template):</p>

<pre><code>documents = Document.objects.all()

if documents:
    exclude_list = []
    for doc in documents:
        try:
            local_file = doc.data.file
        except IOError:
            exclude_list.append(doc.id)
    documents = documents.exclude(id__in=exclude_list)

context_dict['documents'] = documents
</code></pre>

<p>This way the document is not displayed in the template if there is no valid local file found.</p>

python django