It would be quite difficult to determine in the template whether the file actually exists or not. That would be the job of the view, assuming that {{web.image}}
actually returns anything in the first place.
def my_view(request):
# some code here
import os
if os.path.exists(web.image_path): # for example
context = { 'web': web }
render_to_response('my_template.html', context, RequestContext(request)
This assumes you actually know what the full file system path to the image file is. That's not always going to be the case, especially with the staticfiles app in django 1.3.
However, I'd be a lot more concerned that sometimes images exist, and sometimes they don't. Why is that the case? Are images being deleted for some reason? If they are, you need a way of cleaning up the database.
Edit:
Since there doesn't yet seem to be an answer that you like, you should try this:
import os
class MyModel(models.Model):
image = models.Image...
# the rest of your fields
def get_image_url(self):
if os.path.exists(self.image.path):
return u'/%s/img/%s' % (self.app_label, self.image.filename)
return u'%s/img/default.jpg' % self.app_label
In your template, you then do the following:
<img src="{{ STATIC_URL }}{{ web.get_image_url }}" />