【问题标题】:Django 1.4, checking if a static file exists when DEBUG=TrueDjango 1.4,当 DEBUG=True 时检查静态文件是否存在
【发布时间】:2013-03-12 16:30:18
【问题描述】:

我正在尝试编写一些代码来检查在调试模式下是否存在静态文件,但我无法让它工作。这是我在控制台中尝试过的:

>> 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

尽管vi_crumbs_bkg.gif 存在于我的应用程序的静态目录中,但此操作失败。如果我将其符号链接到settings.STATIC_ROOT,则一切正常。

那么,有什么方法可以让staticfiles_storage.exists 在调试模式下工作?

(注意:虽然我知道我可以运行 collectstatic 以使其正常工作,但我不想在积极开发时定期运行它以收集所有应用程序中的所有资产)

提前致谢 -

【问题讨论】:

    标签: django django-staticfiles


    【解决方案1】:

    我想我有一个解决办法。我将不使用staticfiles_storage.exists,而是依靠 Django 的静态文件查找器为我查找文件。像这样:

    >>> 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'
    

    这应该非常适合我的需求,因为我需要做的就是检查一个文件是否存在于一个目录中而不是另一个目录中 - 无论它是在应用程序的根目录中还是 STATIC_ROOT 无关紧要。

    希望这对其他人有所帮助。

    【讨论】:

      猜你喜欢
      • 2019-06-01
      • 2020-10-27
      • 2012-11-27
      • 2016-01-13
      • 2019-12-02
      • 2018-08-24
      • 2011-06-25
      • 1970-01-01
      • 2017-03-31
      相关资源
      最近更新 更多