0

I have multiple apps and each of them has static file. And also I want to add some global static file like jQuery.

My project structure is like

--mysite

  |------app1

     |-----static

  |------app2

     |-----static

  |------app3

     |-----static


  |------media
  |------static    (global one)

     |-----app1
     |-----app2
     |-----app3

my setting file is like

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

MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = '/media/'

and url file is like

urlpatterns = [
url(r'^$', 'home.views.index'),
url(r'^admin/', include(admin.site.urls)),
url(r'^users/', include('users.urls')),
url(r'^projects/', include('cvproject.urls'))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + \
          static(settings.STATIC_URL,     document_root=settings.STATIC_ROOT)

I am developing the web app following the example on the Django website.

When I load static file in templates, it can only find the files in the individual directories. And how can I load the global static files?

Thank you

Mix
  • 459
  • 1
  • 7
  • 20
  • What is the value of TEMPLATE_LOADERS in your settings? – luc Jan 06 '16 at 06:17
  • 1
    just fair warning this is not recommended for production servers ... it is better to use your web layer(apache/nginx/etc) to redirect `/static` to your static files directory – Joran Beasley Jan 06 '16 at 06:35

2 Answers2

1

Make sure you have the FileSystemFinder included in your STATICFILES_FINDERS in settings.py

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder'
)

Then define an additional dir to include additional static files in STATICFILES_DIRS like:

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
fasouto
  • 4,386
  • 3
  • 30
  • 66
  • The staticfiles_dirs is same as static root and returns "ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting" – Mix Jan 06 '16 at 06:23
  • Change it for another folder, both should be different http://stackoverflow.com/questions/12161271/can-i-make-staticfiles-dir-same-as-static-root-in-django-1-3 – fasouto Jan 06 '16 at 06:25
1

Staticfiles will automatically look for static files in multiple locations. and this goes with "finders" ( STATICFILES_FINDERS).

  1. AppDirectoriesFinder : look for "/static/" directory of your apps
  2. STATICFILES_DIRS : the finder that is FileSystemFinder

You have these tools to work with in static files, I think you can do what you want to do with STATICFILES_DIRS. you can find STATICFILES_DIRS examples

P3zhman
  • 243
  • 3
  • 12