Hoa
Hoa

Reputation: 20438

In Django, how can I use project wide static files?

At the moment I have

myproject/app1

And I am adding a second app in and I wish to share some common JS and CSS

I have moved my static files from

myproject/app1/static

to

myproject/static

What configuration changes do I have to make for the applications to recognize the new location? I don't remember ever setting the directory initially I think it just worked out of the box

Upvotes: 3

Views: 1317

Answers (2)

Dušan Maďar
Dušan Maďar

Reputation: 9869

From Django [1.11] Docs:

Your project will probably also have static assets that aren’t tied to a particular app. In addition to using a static/ directory inside your apps, you can define a list of directories (STATICFILES_DIRS) in your settings file where Django will also look for static files.

Add to settings.py:

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]

Assuming you are using django.contrib.staticfiles app.

Upvotes: 6

zeantsoi
zeantsoi

Reputation: 26193

In settings.py, declare the following:

STATIC_URL = '/static/'

Also, add django.contrib.staticfiles to your INSTALLED_APPS if it isn't already.

Upvotes: 0

Related Questions