Django staticfiles app help

后端 未结 5 1385
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-28 18:36

I\'ve having a little issue with Django\'s staticfiles app.

I have added

\'django.contrib.staticfiles\',

to my INSTALLED_APPS and

5条回答
  •  独厮守ぢ
    2020-11-28 19:08

    If you want just a solution - scroll down to the "Solution".

    Overview

    I was discovering the same problem yesterday. Here is what I found:

    All you need is appropriate static finder, that will find your STATIC_ROOT and all its contents, but there is no such finder. Here are default finders:

    • django.contrib.staticfiles.finders.AppDirectoriesFinder - search installed django applications dirs for 'static' folder, but most of them use obsolete 'media' folders for now.

    • django.contrib.staticfiles.finders.FileSystemFinder - use all dirs mentioned in the STATICFILES_DIRS, but you can't add STATIC_ROOT into it.

    • django.contrib.staticfiles.finders.DefaultStorageFinder - search static in your DEFAULT_FILE_STORAGE which is django.core.files.storage.FileSystemStorage by default and it points to your MEDIA_ROOT

    Solution

    That's all, no additional choices. There are no choices to use STATIC_ROOT for now (in Django 1.3).
    So I've just wrote my own custom finder for these needs:

    • My custom static finder file: finders.py:

      from django.core.files.storage import FileSystemStorage
      from django.contrib.staticfiles.finders import BaseStorageFinder
      from django.conf import settings
      
      class StaticFinder(BaseStorageFinder):
          storage = FileSystemStorage(settings.STATIC_ROOT, settings.STATIC_URL)
      
    • settings.py:

      STATICFILES_FINDERS = (
          'finders.StaticFinder',
      )
      
    • If you want to use it with another finders - I suggest to put them after it inside STATICFILES_FINDERS

    And remember: this solution should be used only in development needs and never on production!

提交回复
热议问题