[Django]-Managing static files for multiple apps in Django

57👍

This is the same problem that occurs with using app-specific templates directories. If you just throw the files directly under templates in the app, you’ll end up with name collisions if two apps or even the project-level templates directory utilize templates of the same name. The fix for that is to actually put the templates in a directory of the name of app inside the templates directory like:

- some_app
    - templates
        - some_app
            - index.html

So, I apply the same idea to static:

- some_app
    - static
        - some_app
            - css
            - img
            - js

That way, when you run collectstatic, each individual app’s static files get placed inside a namespaced directory. The only change you need to make is to prefix each of the files with the app-name directory in your templates. So instead of {{ STATIC_URL }}css/style.css you have {{ STATIC_URL }}my_app/css/style.css.

Leave a comment