1

I've multiple directories of static files. Each app has its own static files directory for making it modular. How can I access the static files dirs of all the apps. Initially I was putting all the static files under only one folder. Now I'm keeping the static files inside the apps and then want to access it from inside the app. How do I alter my settings.py file in order to access the static dirs.

Here is my directory structure.

|-- assets                      // static folder named as 'assets'
|   |-- css
|   |   |-- bootstrap.css
|   |   |-- bootstrap.min.css
|   |   |-- bootstrap-responsive.css
|   |   |-- bootstrap-responsive.min.css
|   |   `-- login.css
|   |-- img
|   |   |-- glyphicons-halflings.png
|   |   `-- glyphicons-halflings-white.png
|   `-- js
|       |-- bootstrap.js
|       |-- bootstrap.min.js
|       `-- jquery-1.9.1.min.js

|-- initial                    // My Project Name
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- settings.py
|   |-- settings.pyc
|   |-- urls.py
|   |-- urls.pyc
|   |-- wsgi.py
|   `-- wsgi.pyc
|-- manage.py
|-- models.py
|-- modules                   //apps folder named as 'modules'
|   |-- dashboard
|   |   |-- __init__.py
|   |   |-- __init__.pyc
|   |   |-- models.py
|   |   |-- models.pyc
|   |   |-- static            // static folder inside the dashboard app.
|   |   |   |-- css
|   |   |   |-- img
|   |   |   `-- js
|   |   |       `-- dashboard.js
|   |   |-- templates            // template folder inside the dashboard app.
|   |   |   `-- dashboard
|   |   |       `-- dashboard.html
|   |   |-- tests.py
|   |   |-- urls.py
|   |   |-- urls.pyc
|   |   |-- views.py
|   |   `-- views.pyc
|   |-- login            // login app
|   |   |-- forms.py
|   |   |-- forms.pyc
|   |   |-- __init__.py
|   |   |-- __init__.pyc
|   |   |-- models.py
|   |   |-- models.pyc
|   |   |-- static
|   |   |   |-- css
|   |   |   |   `-- login.css
|   |   |   |-- img
|   |   |   `-- js
|   |   |-- templates
|   |   |   |-- auth
|   |   |   |   |-- login.html
|   |   |   |   |-- logout.html
|   |   |   |   `-- register.html
|   |   |   `-- registration
|   |   |       `-- login.html
|   |   |-- tests.py
|   |   |-- urls.py
|   |   |-- urls.pyc
|   |   |-- views.py
|   |   `-- views.pyc
|  
`-- templates              // templates folder for base templates.
    |-- base1.html
    |-- base2.html
    `-- registration
        `-- login.html

Here is my settings.py file, when all the static files were under one folder.

MEDIA_ROOT = os.path.normpath( os.path.join(os.path.dirname(__file__), '../assets/'))

MEDIA_URL = ''

STATIC_ROOT = ''

STATIC_URL = '/assets/'

Here is my settings.py file, when all the static files were under their respective modules/apps.

MEDIA_ROOT = (
    os.path.normpath( os.path.join(os.path.dirname(__file__), '../assets/')),
    os.path.normpath( os.path.join(os.path.dirname(__file__), '../modules/dashboard/static/')),
    os.path.normpath( os.path.join(os.path.dirname(__file__), '../modules/login/static/')),
    )

MEDIA_URL = ''

STATIC_ROOT = ''

STATIC_URL = '/assets/'
Trevor
  • 16,080
  • 9
  • 52
  • 83
Praful Bagai
  • 16,684
  • 50
  • 136
  • 267
  • Using the Static Files app will help you out here. https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#module-django.contrib.staticfiles - Should help you out. – Henrik Andersson Oct 15 '13 at 06:59
  • MEDIA is for user uploads, STATIC is for, well static files.. (collected by the collectstatic command in production websites, so your server can serve them) If you want to name it assets that's fine, but will cause some extra configuration (the convention is 'static') for further reference: http://stackoverflow.com/questions/11216829/django-directory-structure – Hedde van der Heide Oct 15 '13 at 07:56

1 Answers1

3

You should follow these steps (from the documentation): https://docs.djangoproject.com/en/dev/howto/static-files/

The most important part is:

Store your static files in a folder called static in your app. For example my_app/static/my_app/myimage.jpg.

So change the name from assets to static.

freakish
  • 54,167
  • 9
  • 132
  • 169
  • Why should I change it to `static`? Is it of some relevance? – Praful Bagai Oct 15 '13 at 07:17
  • @user1162512 Because that's how `django.contrib.staticfiles` works. It will search for static files in each app in a subfolder `/static`. It will omit other folders. – freakish Oct 15 '13 at 08:29
  • More specifically that's how `django.contrib.staticfiles.finders.AppDirectoriesFinder` works. You can create your own `STATICFILES_FINDER` in settings if you want to do something custom. The two default finders will search explicit locations (`STATICFILES_DIRS`), or under `/static/` in each installed application. Also keep in mind that the url path will be `STATIC_URL` + filepath after /static. – Tim Edgar Oct 15 '13 at 08:39
  • Though I've got the required o/p, but can I omit `myapp` folder from between. i tried removing it but it does not works the same. Is there any specific reason for it? – Praful Bagai Oct 15 '13 at 09:47