Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Managing static files for multiple apps in Django

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?

like image 211
Andrea Avatar asked Aug 25 '11 14:08

Andrea


People also ask

Can a Django project have multiple apps?

Any Django project consists of multiple applications.

How does Django manage static files?

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.

Can you create more than two Django apps in Django framework?

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.


1 Answers

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.

like image 128
Chris Pratt Avatar answered Oct 05 '22 12:10

Chris Pratt