5

I'm trying to write some code that checks if a static file exists while in DEBUG mode, and I can't get it working. Here's what I've tried in the console:

>> from django.conf import settings
>>> settings.DEBUG
True
>>> from django.contrib.staticfiles.storage import staticfiles_storage
>>> staticfiles_storage.exists('img/vi_crumbs_bkg.gif')
False

This fails, despite vi_crumbs_bkg.gif existing in my app's static directory. If I symlink it to settings.STATIC_ROOT, it all works as expected.

So, is there any way I can get staticfiles_storage.exists to work while in DEBUG mode?

(note: while I know I could just run collectstatic to get this properly working, I'd hate to periodically run that to collect all the assets from all my apps while I'm actively developing)

Thanks in advance -

leo
  • 339
  • 2
  • 13

1 Answers1

8

I think I've got a solution. Instead of using staticfiles_storage.exists, I'm going to rely on Django's staticfile finders to find the file for me. Like so:

>>> from django.contrib.staticfiles import finders
>>> finders.find('img/vi_crumbs_bkg.gif')
u'/home/myuser/django_project/app_root/static/img/vi_crumbs_bkg.gif'

This should be perfect for my needs, since all I need to do is check that a file exists in one directory and not another - regardless of it being in the app's root or the STATIC_ROOT is irrelevant.

Hope this helps someone else.

leo
  • 339
  • 2
  • 13