Aug, 2023 Update:
First of all, I explain about STATIC_ROOT, then [STATIC_URL2.
<STATIC_ROOT>
STATIC_ROOT
can set a folder path and be used with the command below which collects the static files of the apps and admin in a Django project into the folder path. *STATIC_ROOT
never ever influence to STATIC_URL
:
python manage.py collectstatic
And STATIC_ROOT
only works in Production Mode which is DEBUG = False as shown below:
# "core/settings.py"
DEBUG = False // Production Mode
Now, we have a django project with the static files core.js
in core
folder where settings.py
is and myapp.css
in myapp
folder which is an app as shown below:

And css
, fonts
, img
and js
folders in admin
folder in venv
folder as shown below. *I use the virtual environment named venv
for this django project so the static files for admin is in it and the relative path to admin
folder is venv/lib/python3.8/site-packages/django/contrib/admin/static/admin/
:

Then, we set BASE_DIR / 'static/'
which is C:\Users\kai\django-project\static
on Windows in my case to STATIC_ROOT
. In addition, we set DEBUG = False
because STATIC_ROOT
only works in Production Mode as I said before:
# "core/settings.py"
DEBUG = False // Production Mode
STATIC_ROOT = BASE_DIR / 'static/' # Here
STATIC_URL = 'static/'
Now, we run the command below:
python manage.py collectstatic
Then, static
folder is created and css
, fonts
, img
and js
folders in admin
folder** and myapp.css
in myapp
folder are collected into static
folder as shown below but as we can see, core.js
in core
folder is not collected into static
folder as shown below because as I said before, the command python manage.py collectstatic
collects static files from the apps and admin in a django project but core
folder which has settings.py
is not an app and admin. That's why core.js
in core
folder is not collected into static
folder:

But, there is a way to collect core.js
in core
folder into static
folder. To do that, we need to set BASE_DIR / 'core/static/'
which is C:\Users\kai\django-project\core\static
on Windows in my case to STATICFILES_DIRS in settings.py
as shown below:
# "core/settings.py"
DEBUG = False
STATICFILES_DIRS = [ # Here
BASE_DIR / 'core/static/'
]
STATIC_ROOT = BASE_DIR / 'static/'
STATIC_URL = 'static/'
Or, we need to set core
to INSTALLED_APPS in settings.py
as shown below:
# "core/settings.py"
DEBUG = False
INSTALLED_APPS = [
'core', # Here
]
Now again, we run the command below:
python manage.py collectstatic
Then, input yes
then press Enter to overwrite the existing static
folder:
You have requested to collect static files at the destination location
as specified in your settings:
C:\Users\kai\django-project\static
This will overwrite existing files! Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: yes
Now, core.js
in core
folder is collected into static
folder as shown below:

<STATIC_URL>
Next, I explain about STATIC_URL
.
STATIC_URL
can set the front directory part of static file URL between the host part and the file part of static file URL as shown below. *STATIC_URL
never ever influence STATIC_ROOT
:
| Host | Directory | File |
| |Front | Back | |
<-------------> <----> <-------> <------>
https://www.example.com/static/admin/css/base.css
For example, we set 'static/'
to STATIC_URL
in settings.py
as shown below. *STATIC_URL
works in both Development Mode which is DEBUG = True
and Production Mode which is DEBUG = False
:
# "core/settings.py"
DEBUG = False
STATICFILES_DIRS = [
BASE_DIR / 'core/static/'
]
STATIC_ROOT = BASE_DIR / 'static/'
STATIC_URL = 'static/' # Here
Then, open Django Admin:

Then, press F12 to open Developer Tools to check the resources used for the currently opened Django Admin page from Sources and there are the static files for admin which we have just collected into static
folder:

Then, hover base.css
in css
to check the URL:

As we can see, we could set the front directory part static
:
Here
<------>
http://localhost:8000/static/admin/css/base.css
And, this URL below is in this case of www.example.com
with https
:
Here
<------>
https://www.example.com/static/admin/css/base.css
And, we can change the front directory part static
to hello/world
.
So, just change STATIC_URL
from 'static/'
to 'hello/world/'
as shown below:
# "core/settings.py"
DEBUG = False
STATICFILES_DIRS = [
BASE_DIR / 'core/static/'
]
STATIC_ROOT = BASE_DIR / 'static/'
STATIC_URL = 'hello/world/' # Here
Then, refresh the Django Admin page:

Then, the front directory part static
is changed to hello/world
as shown below:
Here
<----------->
http://localhost:8000/hello/world/admin/css/base.css