Praful Bagai
Praful Bagai

Reputation: 17392

Linking static files directory in Django

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/'

Upvotes: 1

Views: 2743

Answers (1)

freakish
freakish

Reputation: 56527

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.

Upvotes: 3

Related Questions