Sunny Mukesh
Sunny Mukesh

Reputation: 41

Can we use multiple static_url or static_root in django settings?

I want to add specific assets in a folder and should be able to access with url like /assets/css/main.css Am able to do this with /static/css/main.css but not with /assets/css/main.css

What changes do I need to do for django to accept /assets/ path too.

Default -

STATIC_ROOT = 'app/static'
STATIC_URL = '/static/'

for accessing both static and assets folders-

STATIC_ROOT = 'app/static'
STATIC_URL = '/static/'

ASSET_ROOT = 'app/assets'
ASSET_URL = '/assets/'

and I tried this too -

STATIC_ROOT = 'app/static'
STATIC_URL = '/static/'

ASSET_ROOT = os.path.join(BASE_DIR, 'app/assets')
ASSET_URL = '/assets/'

Upvotes: 4

Views: 2991

Answers (2)

Toluwalemi
Toluwalemi

Reputation: 414

Why not have a single static folder for all your static files? And if you have multiple apps you're working with, all you'll need to do is create directories inside your main static folder.

Now Looking at the details of your question, I suggest you create two sub-directories inside your main static file. Something like this:

static/
    main/
      css/
         main.css
    assets/
      css/
         main.css

Then in your settings.py file do this:

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

Now to access static files in your template, you do something like this:

{% load static %}

<link href="{% static "assets/css/main.css" %}" rel="stylesheet">
<link href="{% static "main/css/main.css" %}" rel="stylesheet">

Upvotes: 2

Enderson Menezes
Enderson Menezes

Reputation: 479

If at the end all your files are within 'app' folder, by what I understood your ROOT would be the 'app' folder, you can create other variables and import with from .setting import X...

Upvotes: 0

Related Questions