[Django]-Django: Where to store global static files and templates?

32👍

Assume there is a global css file located here:
home/user/myproject/staticfiles/css/global.css

Change settings.py to match this:

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "staticfiles"), 
]

Inside of a template, for example, index.html:

{% load static %}

<link rel="stylesheet" type="text/css" href="{% static 'css/global.css' %}" />   

Now Django can find global.css.

1👍

in your settings.py file add the following

STATIC_URL = '/static/' # this will be added before your file name
STATIC_ROOT = 'path/to/your/files/' # this will help django to locate the files

read more about it here: https://docs.djangoproject.com/en/1.9/howto/static-files/

1👍

While nu everest’s answer is correct and working (after you restart the server), in my case it lacks the advantage of my IDE autosuggesting the static files’ paths. So I went for specifying a relative path inside my project. For simplicity I named the directory ‘static’ (same as the STATIC_URL variable) but of course any name is possible.

settings.py:

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    "myproject" + STATIC_URL
]

What’s important here: The root is actually not the settings.py‘s parent, but its parents’s parent, as in:

BASE_DIR = Path(__file__).resolve().parent.parent
👤david

Leave a comment