I am developing a Django (1.3) project composed of many applications. Each application has its own static files under its own static
directory. Moreover, I have added a directory called project_static
which should contain static files which are common throughout the various applications, such as jQuery.
The problem I immediately run into is that of naming collisions. By default, collectstatic
will just put everything under a global static
directory, without classifying them by application. This does not work for me, as each application has - for instance - a file called css/screen.css
.
The way I solved it is by removing django.contrib.staticfiles.finders.AppDirectoriesFinder
from STATICFILES_FINDERS
and using namespaced static files dirs, so my settings now look like:
STATICFILES_DIRS = ( os.path.join(PROJECT_PATH, 'project_static'), ('my_app', os.path.join(PROJECT_PATH, 'my_app', 'static')), ('another_app', os.path.join(PROJECT_PATH, 'another_app', 'static')), ... ) STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', )
The problem is that in this way I lose the static files for all other applications (e.g. django.contrib.admin
). Of course I could manually add the admin, but I'm not sure how to do this without breaking the admin, which has a different directory structure.
Is there a better way to manage the static files with more than one application?
Any Django project consists of multiple applications.
Configuring static filesMake sure that django.contrib.staticfiles is included in your INSTALLED_APPS . In your templates, use the static template tag to build the URL for the given relative path using the configured STATICFILES_STORAGE . Store your static files in a folder called static in your app.
If you want. Or split it up into two view files - views are just Python functions, they can live anywhere. Or have a separate app for all the admin functions across all your other apps. It's up to you.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With