Vishal
Vishal

Reputation: 2336

Setting up site-specific static folders with Django Sites

I am using Django Sites. I want to be able to have site-specific templates and static files.

My current directory structure is:

├── site_a
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   ├── views.py
│   └── wsgi.py
├── site_b
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   ├── views.py
│   └── wsgi.py
└── templates
    ├── site_a
    │   ├── _navbar.html
    │   ├── home.html
    │   └── signup.html
    └── site_b
        ├── _navbar.html
        ├── home.html
        └── signup.html

I know that I can move templates inside the site_x directory if I declare it as an app in INSTALLED_APPS. Is there another way to tell Django to use templates in site_a/templates without declaring site_a as an app?

Also, I would also like to have site specific static files that are not placed in the project STATIC_ROOT so that my tree actually looks like:

.
├── site_a
│   ├── __init__.py
│   ├── settings.py
│   ├── static
│   │   ├── css
│   │   └── js
│   ├── templates
│   │   ├── _navbar.html
│   │   └── home.html
│   ├── urls.py
│   ├── views.py
│   └── wsgi.py
└── site_b
    ├── __init__.py
    ├── settings.py
    ├── static
    │   ├── css
    │   └── js
    ├── templates
    │   ├── _navbar.html
    │   └── home.html
    ├── urls.py
    ├── views.py
    └── wsgi.py

Upvotes: 2

Views: 55

Answers (1)

NKSM
NKSM

Reputation: 5874

You can setting static files via STATICFILES_DIRS (Django Docs) without declaring site_a:

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
    ('site_a', os.path.join(BASE_DIR, 'site_a/static')),
    'Full path to your file or directory'
)

And in template:

<script src="{% static 'site_a/js/my_site_a.js' %}" type="text/javascript"></script>

And with declaring of your app:

Store your static files(Django Docs) in a folder called static in your app: site_a/static/site_a/example.jpg.

And for templates same: site_a/templates/site_a/example.html

in your settings.py set APP_DIRS:

TEMPLATES = [
    {
        ...,
        'APP_DIRS': True,
        ...
    },
]

See Support for template engines and Overriding templates:

APP_DIRS tells whether the engine should look for templates inside installed applications. Each backend defines a conventional name for the subdirectory inside applications where its templates should be stored.

Upvotes: 1

Related Questions