I am using Django 2.2. From the Django Managing static files documentation:
If you use django.contrib.staticfiles as explained above, runserver will do this automatically when DEBUG is set to True. If you don’t have django.contrib.staticfiles in INSTALLED_APPS, you can still manually serve static files using the django.views.static.serve() view.
This is not suitable for production use! For some common deployment strategies, see Deploying static files.
For example, if your STATIC_URL is defined as /static/, you can do this by adding the following snippet to your urls.py:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Note
This helper function works only in debug mode and only if the given prefix is local (e.g. /static/) and not a URL (e.g. http://static.example.com/).
Also this helper function only serves the actual STATIC_ROOT folder; it doesn’t perform static files discovery like django.contrib.staticfiles.
My Interpretation
- static is a helper function that serves files from the
STATIC_ROOTduring development (is this True?) - static only works when
debug = True - static only works with a local prefix like
STATIC_URL = '/static/' - When
DEBUGis set to True and I use and setup the staticfiles app as explained in the documentation, if I dopython manage.py runserverto start the local server, the serving of static files will be handled automatically (true??)
My Questions
- What EXACTLY does adding
static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)to your project'surls.pyDO? - Is it true that static serves static files locally from the
STATIC_ROOTdirectory? To test this theory, after runningcollectstatic, I then deleted thestaticdirectories to see if the static files still load fine (from the STATIC_ROOT) and they DON'T! Why? - How can I verify that Django is loading the static files from my STATIC_ROOT location... and not the static directories in my project and apps??
- Why is adding
static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)tourlpatternsnecessary if Django serves static files automatically (mentioned in documentation)?
Example
settings.py
DEBUG = True
...
INSTALLED_APPS = [
'django.contrib.admin',
...
'django.contrib.staticfiles',
'puppies.apps.PuppiesConfig'
]
...
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = 'c:/lkdsjfkljsd_cdn'
In all my templates, I'm using {% load static %}.
Then I do: python manage.py collectstatic
At this point, it doesn't seem to matter if I have the below in my urls.py or not - my static files still load BUT I don't know if they're coming from my project's static directories or my STATIC_ROOT (c:/lkdsjfkljsd_cdn):
if settings.DEBUG is True:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Lastly, if I delete those static directories in my project, all css, js and images don't work which leads me to believe that my Django project is loading static files from my project's static directories, NOT the STATIC_ROOT.
So, again, how can I tell Django to load the static files from my STATIC_ROOT location... and not the static directories in my project and apps?? OR, do I misunderstand that Django isn't supposed to load files from my STATIC_ROOT location locally?
*Edit (adding HTML image)


