Search code examples
pythondjangocollectstatic

django collectstatic does not exclude external folder


I located my client project folder and django project folder under the root folder separately. I configured django settings.py to contain client's app folder to serve in development and dist folder to be gathered by collectstatic

STATICFILES_DIRS = [os.path.join(BASE_DIR, "..", "Client/app"), # in development
                    os.path.join(BASE_DIR, "..", "Client/dist"), # to be gathered by collectstatic 
                   ]

Client/app contains original js/css/html files for development and Client/dist contains concatenated and uglified files for production.

Because Client/app folder is only for development, I want to exclude the folder when I use collectstaic command.

However, collectstatic -i app does not exclude Client/app folder. I tried

collectstatic -i Client/app
collectstatic -i ../Client/app
collectstatic -i app*

but, nothing did work.

How can I exclude folder outside django directory?


Solution

  • You would not do that normally. You would define a different STATICFILES_DIR depending on what environment you run.

    Very basic idea:

    if DEBUG:
        STATICFILES_DIRS = [os.path.join(BASE_DIR, "..", "Client/app")]
    else:
        STATICFILES_DIRS = [os.path.join(BASE_DIR, "..", "Client/dist")]
    

    Instead of relying on the DEBUG setting though, I'd recommend you use a separate config file for each. You then choose which to run when you invoke Django.

    For instance, assuming this file tree:

    settings/
        ├ __init__.py     # empty
        ├ dev.py
        └ prod.py
    

    …you'd start Django this way:

    export DJANGO_SETTINGS_MODULE="settings.dev"
    ./manage.py runserver
    

    To avoid repeating shared configuration, create a common.py and import it from both dev.py and prod.py, using from settings.common import * (probably the only use case where it's permissible to import *).

    Though it does not technically answer your question, I think this is a cleaner approach to the wider problem of handling environment-specific configuration.