Django does not support STATIC_ROOT in DEBUG

I use Python 3.5 and Django 1.10 to start the development server:

./manage.py runserver 0.0.0.0:8000

In mine settings.pyI have:

DEBUG       = True
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL  = '/static/'

And a directory appwith a subdirectory staticfor its static files:

proj/
    proj/
        ...
    app/
        static/
            a.txt
        ...
    static/
        b.txt

Pretty standard.

However: Django does not serve STATIC_ROOTwhenDEBUG = True . It returns app/static/a.txtfor /static/a.txtin order, but not static/b.txtfor /static/b.txt.

Change settings.pyas follows:

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

Works - but then I have to comment STATIC_ROOT(otherwise Django complains that he may not be in STATICFILES_DIRS).

" ", . static2, django-sass-processor, .sass .css .css STATIC_ROOT (, , ).

, :

  • NGINX (, ). , .

  • django-sass-processor .css " ", . static2, STATICFILES_DIRS. , , !

  • URL- urls.py:

    if settings.DEBUG:
        urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    

    , , : , Django , URL-, , , , STATICFILES_DIRS.

    DEBUG False if - , django.conf.urls.static.static . , django.views.static.serve, , , , - , .

  1. django-sass-processor , , : , settings.py :

    STATICFILES_FINDERS = [
        'django.contrib.staticfiles.finders.FileSystemFinder',
        'django.contrib.staticfiles.finders.AppDirectoriesFinder',
        'sass_processor.finders.CssFinder',
    ]
    

    ( - Django, , , ).

    , - , .css , STATIC_ROOT , /static/, . , ( ...) .

+4
1

, Django STATIC_ROOT, , Django . Django , , . , manage.py collectstatic, STATICFILES_FINDERS ( ), AWS S3 - .

, :

  • , DEBUG
  • , collectstatic

STATIC_ROOT STATICFILES_DIRS, collectstatic, , . , Django STATIC_ROOT, .

, STATIC_ROOT. Celery, . , AWS S3, .

, STATIC_ROOT, STATICFILES_FINDERS:

from django.contrib.staticfiles.finders import BaseFinder
from django.contrib.staticfiles.storage import staticfiles_storage

class StaticRootFinder(BaseFinder):
    """
    For debug mode only. Serves files from STATIC_ROOT.
    """
    def find(self, path, all=False):
        full_path = staticfiles_storage.path(path)
        if staticfiles_storage.exists(full_path):
            return [full_path] if all else full_path
        return []

    def list(self, ignore_patterns):
        return iter(())
+2

Source: https://habr.com/ru/post/1659280/


All Articles